瀏覽代碼

added attr 'operstate'

Fabian Peter Hammerle 7 年之前
父節點
當前提交
56a232026a
共有 4 個文件被更改,包括 73 次插入0 次删除
  1. 3 0
      README.md
  2. 5 0
      link.cpp
  3. 2 0
      link.h
  4. 63 0
      operstate.h

+ 3 - 0
README.md

@@ -16,11 +16,14 @@ $ ipyml
 - ifname: lo
   hwaddr: 00:00:00:00:00:00
   broadcast: 00:00:00:00:00:00
+  operstate: unknown
 - ifname: eth0
   hwaddr: 11:22:33:44:55:66
   broadcast: ff:ff:ff:ff:ff:ff
+  operstate: down
 - ifname: wlan0
   hwaddr: 77:88:99:00:aa:bb
   broadcast: ff:ff:ff:ff:ff:ff
+  operstate: up
 
 ```

+ 5 - 0
link.cpp

@@ -18,6 +18,9 @@ int Link::mnl_attr_cb(const nlattr *attr, void *data) {
   case IFLA_IFNAME:
     link->ifname = mnl_attr_get_str(attr);
     break;
+  case IFLA_OPERSTATE:
+    link->operstate = attr;
+    break;
   }
   return MNL_CB_OK;
 }
@@ -32,4 +35,6 @@ void Link::write_yaml(std::ostream &stream,
   stream << indent + "broadcast: ";
   broadcast.write_yaml(stream);
   stream << "\n";
+  stream << indent + "operstate: ";
+  operstate.write_yaml(stream);
 }

+ 2 - 0
link.h

@@ -2,6 +2,7 @@
 #define _IPYML_LINK_H
 
 #include "hardware_address.h"
+#include "operstate.h"
 #include "yaml.h"
 
 #include <linux/netlink.h>
@@ -11,6 +12,7 @@
 class Link : public YamlObject {
   std::string ifname;
   HardwareAddress hwaddr, broadcast;
+  OperState operstate;
 
 public:
   // typedef int (*mnl_attr_cb_t)(const struct nlattr *attr, void *data);

+ 63 - 0
operstate.h

@@ -0,0 +1,63 @@
+#ifndef _IPYML_OPERSTATE_H
+#define _IPYML_OPERSTATE_H
+
+#include "yaml.h"
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/netlink.h>
+
+class OperState : public YamlObject {
+  // RFC 2863 operational status
+  /* linux/if.h:
+  enum {
+      IF_OPER_UNKNOWN,
+      IF_OPER_NOTPRESENT,
+      IF_OPER_DOWN,
+      IF_OPER_LOWERLAYERDOWN,
+      IF_OPER_TESTING,
+      IF_OPER_DORMANT,
+      IF_OPER_UP,
+  };
+  */
+  unsigned char state;
+
+public:
+  OperState &operator=(const nlattr *attr) {
+    assert(mnl_attr_get_payload_len(attr) == 1);
+    this->state = mnl_attr_get_u8(attr);
+    return *this;
+  }
+
+  void write_yaml(std::ostream &stream,
+                  const yaml_indent_level_t indent_level = 0) const {
+    const char *label = NULL;
+    switch (state) {
+    case IF_OPER_UNKNOWN:
+      label = "unknown";
+      break;
+    case IF_OPER_NOTPRESENT:
+      label = "not present";
+      break;
+    case IF_OPER_DOWN:
+      label = "down";
+      break;
+    case IF_OPER_LOWERLAYERDOWN:
+      label = "lower layer down";
+      break;
+    case IF_OPER_TESTING:
+      label = "testing";
+      break;
+    case IF_OPER_DORMANT:
+      label = "dormant";
+      break;
+    case IF_OPER_UP:
+      label = "up";
+      break;
+    }
+    assert(label);
+    stream << label << "\n";
+  }
+};
+
+#endif