use running linux version number for feature availability checking
authorYonghong Song <yhs@plumgrid.com>
Fri, 17 Jul 2015 03:20:22 +0000 (20:20 -0700)
committerYonghong Song <yhs@plumgrid.com>
Fri, 17 Jul 2015 03:20:22 +0000 (20:20 -0700)
Signed-off-by: Yonghong Song <yhs@plumgrid.com>
src/cc/b_frontend_action.cc

index 45c0f25..9ddca05 100644 (file)
@@ -15,6 +15,7 @@
  */
 #include <linux/bpf.h>
 #include <linux/version.h>
+#include <sys/utsname.h>
 
 #include <clang/AST/ASTConsumer.h>
 #include <clang/AST/ASTContext.h>
@@ -376,10 +377,16 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
       map_type = BPF_MAP_TYPE_HASH;
     else if (A->getName() == "maps/array")
       map_type = BPF_MAP_TYPE_ARRAY;
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,2,0)
-    else if (A->getName() == "maps/prog")
-      map_type = BPF_MAP_TYPE_PROG_ARRAY;
-#endif
+    else if (A->getName() == "maps/prog") {
+      struct utsname un;
+      if (uname(&un) == 0) {
+        int major = 0, minor = 0;
+        // release format: <major>.<minor>.<revision>[-<othertag>]
+        sscanf(un.release, "%d.%d.", &major, &minor);
+        if (KERNEL_VERSION(major,minor,0) >= KERNEL_VERSION(4,2,0))
+          map_type = BPF_MAP_TYPE_PROG_ARRAY;
+      }
+    }
     table.fd = bpf_create_map(map_type, table.key_size, table.leaf_size, table.max_entries);
     if (table.fd < 0) {
       llvm::errs() << "error: could not open bpf fd\n";