lldpd-structs: Declare Boolean member as one bit, not two.
[cascardo/ovs.git] / lib / lldp / lldpd.c
index 9c8173e..f3d142e 100644 (file)
@@ -1,5 +1,6 @@
 /* -*- mode: c; c-file-style: "openbsd" -*- */
 /*
+ * Copyright (c) 2015 Nicira, Inc.
  * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
@@ -29,7 +30,6 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <time.h>
 #include <unistd.h>
 #ifndef _WIN32
 #include <grp.h>
@@ -42,6 +42,7 @@
 #include "compiler.h"
 #include "list.h"
 #include "packets.h"
+#include "timeval.h"
 
 VLOG_DEFINE_THIS_MODULE(lldpd);
 
@@ -65,9 +66,8 @@ lldpd_get_hardware(struct lldpd *cfg, char *name, int index,
     struct lldpd_hardware *hw;
 
     LIST_FOR_EACH (hw, h_entries, &cfg->g_hardware.h_entries) {
-        if ((strcmp(hw->h_ifname, name) == 0) &&
-            (hw->h_ifindex == index) &&
-            ((!ops) || (ops == hw->h_ops))) {
+        if (!strcmp(hw->h_ifname, name) && hw->h_ifindex == index
+            && (!ops || ops == hw->h_ops)) {
             return hw;
         }
     }
@@ -82,17 +82,14 @@ lldpd_alloc_hardware(struct lldpd *cfg, char *name, int index)
 
     VLOG_DBG("allocate a new local hardware interface (%s)", name);
 
-    if ((hw = (struct lldpd_hardware *) calloc(1, sizeof *hw)) == NULL) {
-        return NULL;
-    }
-
+    hw = xzalloc(sizeof *hw);
     hw->h_cfg = cfg;
     ovs_strlcpy(hw->h_ifname, name, sizeof hw->h_ifname);
     hw->h_ifindex = index;
-    hw->h_lport.p_chassis = (struct lldpd_chassis *)
-    list_front(&cfg->g_chassis.list);
+    hw->h_lport.p_chassis = CONTAINER_OF(list_front(&cfg->g_chassis.list),
+                                         struct lldpd_chassis, list);
     hw->h_lport.p_chassis->c_refcount++;
-    list_init(&hw->h_rports.p_entries);
+    list_init(&hw->h_rports);
 
     return hw;
 }
@@ -112,11 +109,7 @@ lldpd_alloc_mgmt(int family, void *addrptr, size_t addrsize, u_int32_t iface)
         errno = EOVERFLOW;
         return NULL;
     }
-    mgmt = calloc(1, sizeof *mgmt);
-    if (mgmt == NULL) {
-        errno = ENOMEM;
-        return NULL;
-    }
+    mgmt = xzalloc(sizeof *mgmt);
     mgmt->m_family = family;
     memcpy(&mgmt->m_addr, addrptr, addrsize);
     mgmt->m_addrsize = addrsize;
@@ -130,7 +123,7 @@ lldpd_hardware_cleanup(struct lldpd *cfg, struct lldpd_hardware *hardware)
 {
     VLOG_DBG("cleanup hardware port %s", hardware->h_ifname);
 
-    lldpd_port_cleanup(&hardware->h_lport, 1);
+    lldpd_port_cleanup(&hardware->h_lport, true);
     if (hardware->h_ops && hardware->h_ops->cleanup) {
         hardware->h_ops->cleanup(cfg, hardware);
     }
@@ -148,10 +141,10 @@ lldpd_cleanup(struct lldpd *cfg)
     LIST_FOR_EACH_SAFE (hw, hw_next, h_entries, &cfg->g_hardware.h_entries) {
         if (!hw->h_flags) {
             list_remove(&hw->h_entries);
-            lldpd_remote_cleanup(hw, NULL, 1);
+            lldpd_remote_cleanup(hw, NULL, true);
             lldpd_hardware_cleanup(cfg, hw);
         } else {
-            lldpd_remote_cleanup(hw, NULL, 0);
+            lldpd_remote_cleanup(hw, NULL, false);
         }
     }
 
@@ -187,15 +180,12 @@ lldpd_move_chassis(struct lldpd_chassis *ochassis,
      * marshaling.
      */
     memcpy(ochassis, chassis, sizeof *ochassis);
-    list_init(&ochassis->c_mgmt.m_entries);
+    list_init(&ochassis->c_mgmt);
 
     /* Copy of management addresses */
-    LIST_FOR_EACH_SAFE (mgmt,
-                        mgmt_next,
-                        m_entries,
-                        &chassis->c_mgmt.m_entries) {
+    LIST_FOR_EACH_SAFE (mgmt, mgmt_next, m_entries, &chassis->c_mgmt) {
         list_remove(&mgmt->m_entries);
-        list_insert(&ochassis->c_mgmt.m_entries, &mgmt->m_entries);
+        list_insert(&ochassis->c_mgmt, &mgmt->m_entries);
     }
 
     /* Restore saved values */
@@ -248,7 +238,7 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
     int guess = LLDPD_MODE_LLDP;
     struct eth_header eheader;
     int count = 0;
-    int found = 0;
+    bool found = false;
 
     VLOG_DBG("decode a received frame on %s size %d", hw->h_ifname,s);
 
@@ -268,13 +258,13 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
         s -= 4;
     }
 
-    LIST_FOR_EACH (oport, p_entries, &hw->h_rports.p_entries) {
-        if ((oport->p_lastframe != NULL) &&
-            (oport->p_lastframe->size == s) &&
-            (memcmp(oport->p_lastframe->frame, frame, s) == 0)) {
+    LIST_FOR_EACH (oport, p_entries, &hw->h_rports) {
+        if (oport->p_lastframe &&
+            oport->p_lastframe->size == s &&
+            !memcmp(oport->p_lastframe->frame, frame, s)) {
             /* Already received the same frame */
             VLOG_DBG("duplicate frame, no need to decode");
-            oport->p_lastupdate = time(NULL);
+            oport->p_lastupdate = time_now();
             return;
         }
     }
@@ -310,19 +300,19 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
     /* Do we already have the same MSAP somewhere? */
     VLOG_DBG("search for the same MSAP");
 
-    LIST_FOR_EACH (oport, p_entries, &hw->h_rports.p_entries) {
+    LIST_FOR_EACH (oport, p_entries, &hw->h_rports) {
         if (port->p_protocol == oport->p_protocol) {
             count++;
-            if ((port->p_id_subtype == oport->p_id_subtype) &&
-                (port->p_id_len == oport->p_id_len) &&
-                (memcmp(port->p_id, oport->p_id, port->p_id_len) == 0) &&
-                (chassis->c_id_subtype == oport->p_chassis->c_id_subtype) &&
-                (chassis->c_id_len == oport->p_chassis->c_id_len) &&
-                (memcmp(chassis->c_id, oport->p_chassis->c_id,
-                chassis->c_id_len) == 0)) {
+            if (port->p_id_subtype == oport->p_id_subtype &&
+                port->p_id_len == oport->p_id_len &&
+                !memcmp(port->p_id, oport->p_id, port->p_id_len) &&
+                chassis->c_id_subtype == oport->p_chassis->c_id_subtype &&
+                chassis->c_id_len == oport->p_chassis->c_id_len &&
+                !memcmp(chassis->c_id, oport->p_chassis->c_id,
+                        chassis->c_id_len)) {
                 ochassis = oport->p_chassis;
                 VLOG_DBG("MSAP is already known");
-                found = 1;
+                found = true;
                 break;
             }
         }
@@ -342,8 +332,8 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
         } else if (count > cfg->g_config.c_max_neighbors - 1) {
             VLOG_DBG("too many neighbors for port %s, drop this new one",
                      hw->h_ifname);
-            lldpd_port_cleanup(port, 1);
-            lldpd_chassis_cleanup(chassis, 1);
+            lldpd_port_cleanup(port, true);
+            lldpd_chassis_cleanup(chassis, true);
             free(port);
             return;
         }
@@ -351,7 +341,7 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
 
     /* No, but do we already know the system? */
     if (!oport) {
-        int found = 0;
+        bool found = false;
         VLOG_DBG("MSAP is unknown, search for the chassis");
 
         LIST_FOR_EACH (ochassis, list, &cfg->g_chassis.list) {
@@ -360,7 +350,7 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
                     (chassis->c_id_len == ochassis->c_id_len) &&
                     (memcmp(chassis->c_id, ochassis->c_id,
                     chassis->c_id_len) == 0)) {
-                    found=1;
+                    found = true;
                     break;
                 }
         }
@@ -391,12 +381,11 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
     }
 
     /* Add port */
-    port->p_lastchange = port->p_lastupdate = time(NULL);
-    if ((port->p_lastframe = malloc(s + sizeof(struct lldpd_frame))) != NULL) {
-        port->p_lastframe->size = s;
-        memcpy(port->p_lastframe->frame, frame, s);
-    }
-    list_insert(&hw->h_rports.p_entries, &port->p_entries);
+    port->p_lastchange = port->p_lastupdate = time_now();
+    port->p_lastframe = xmalloc(s + sizeof(struct lldpd_frame));
+    port->p_lastframe->size = s;
+    memcpy(port->p_lastframe->frame, frame, s);
+    list_insert(&hw->h_rports, &port->p_entries);
 
     port->p_chassis = chassis;
     port->p_chassis->c_refcount++;
@@ -413,8 +402,7 @@ lldpd_decode(struct lldpd *cfg, char *frame, int s,
      * freed with lldpd_port_cleanup() and therefore, the refcount
      * of the chassis that was attached to it is decreased.
      */
-    /* coverity[use_after_free] TAILQ_REMOVE does the right thing */
-    i = list_size((struct ovs_list *) &hw->h_rports);
+    i = list_size(&hw->h_rports);
     VLOG_DBG("%"PRIuSIZE " neighbors for %s", i, hw->h_ifname);
 
     if (!oport)  {
@@ -431,7 +419,8 @@ lldpd_hide_ports(struct lldpd *cfg,
     struct lldpd_port *port;
     int protocols[LLDPD_MODE_MAX + 1];
     char buffer[256];
-    int i, j, k, found = 0;
+    bool found = false;
+    int i, j, k;
     unsigned int min;
 
     VLOG_DBG("apply smart filter for port %s", hw->h_ifname);
@@ -441,7 +430,7 @@ lldpd_hide_ports(struct lldpd *cfg,
         protocols[i] = 0;
     }
 
-    LIST_FOR_EACH (port, p_entries, &hw->h_rports.p_entries) {
+    LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
         protocols[port->p_protocol]++;
     }
 
@@ -456,11 +445,11 @@ lldpd_hide_ports(struct lldpd *cfg,
         }
     }
     for (i = 0; i <= LLDPD_MODE_MAX; i++) {
-        if ((protocols[i] == min) && !found) {
+        if (protocols[i] == min && !found) {
             /* If we need a tie breaker, we take the first protocol only */
             if (cfg->g_config.c_smart & mask &
                 (SMART_OUTGOING_ONE_PROTO | SMART_INCOMING_ONE_PROTO)) {
-                found = 1;
+                found = true;
             }
             protocols[i] = 1;
         } else {
@@ -469,34 +458,34 @@ lldpd_hide_ports(struct lldpd *cfg,
     }
 
     /* We set the p_hidden flag to 1 if the protocol is disabled */
-    LIST_FOR_EACH (port, p_entries, &hw->h_rports.p_entries) {
+    LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
         if (mask == SMART_OUTGOING) {
-            port->p_hidden_out = protocols[port->p_protocol] ? 0 : 1;
+            port->p_hidden_out = protocols[port->p_protocol] ? false : true;
         } else {
-            port->p_hidden_in = protocols[port->p_protocol] ? 0 : 1;
+            port->p_hidden_in = protocols[port->p_protocol] ? false : true;
         }
     }
 
     /* If we want only one neighbor, we take the first one */
     if (cfg->g_config.c_smart & mask &
         (SMART_OUTGOING_ONE_NEIGH | SMART_INCOMING_ONE_NEIGH)) {
-        found = 0;
+        found = false;
 
-        LIST_FOR_EACH (port, p_entries, &hw->h_rports.p_entries) {
+        LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
             if (mask == SMART_OUTGOING) {
                 if (found) {
-                    port->p_hidden_out = 1;
+                    port->p_hidden_out = true;
                 }
                 if (!port->p_hidden_out) {
-                    found = 1;
+                    found = true;
                 }
             }
             if (mask == SMART_INCOMING) {
                 if (found) {
-                    port->p_hidden_in = 1;
+                    port->p_hidden_in = true;
                 }
                 if (!port->p_hidden_in) {
-                    found = 1;
+                    found = true;
                 }
             }
         }
@@ -508,9 +497,9 @@ lldpd_hide_ports(struct lldpd *cfg,
     }
 
     k = j = 0;
-    LIST_FOR_EACH (port, p_entries, &hw->h_rports.p_entries) {
-        if (!(((mask == SMART_OUTGOING) && port->p_hidden_out) ||
-              ((mask == SMART_INCOMING) && port->p_hidden_in))) {
+    LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
+        if (!((mask == SMART_OUTGOING && port->p_hidden_out) ||
+              (mask == SMART_INCOMING && port->p_hidden_in))) {
             k++;
             protocols[port->p_protocol] = 1;
         }
@@ -585,7 +574,7 @@ lldpd_recv(struct lldpd *cfg,
 }
 
 uint32_t
-lldpd_send(struct lldpd_hardware *hw, struct ofpbuf *p)
+lldpd_send(struct lldpd_hardware *hw, struct dp_packet *p)
 {
     struct lldpd *cfg = hw->h_cfg;
     struct lldpd_port *port;
@@ -609,7 +598,7 @@ lldpd_send(struct lldpd_hardware *hw, struct ofpbuf *p)
         /* We send only if we have at least one remote system
          * speaking this protocol or if the protocol is forced */
         if (cfg->g_protocols[i].enabled > 1) {
-            if ((lldp_size = cfg->g_protocols[i].send(cfg, hw, p)) != E2BIG) {
+            if ((lldp_size = cfg->g_protocols[i].send(cfg, hw, p)) != -E2BIG) {
                 sent++;
                 continue;
             } else {
@@ -618,7 +607,7 @@ lldpd_send(struct lldpd_hardware *hw, struct ofpbuf *p)
             }
         }
 
-        LIST_FOR_EACH (port, p_entries, &hw->h_rports.p_entries) {
+        LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
             /* If this remote port is disabled, we don't consider it */
             if (port->p_hidden_out) {
                 continue;