/* -*- 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
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
-#include <time.h>
#include <unistd.h>
#ifndef _WIN32
#include <grp.h>
#include "compiler.h"
#include "list.h"
#include "packets.h"
+#include "timeval.h"
VLOG_DEFINE_THIS_MODULE(lldpd);
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;
}
}
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;
}
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;
{
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);
}
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);
}
}
* 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 */
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);
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;
}
}
/* 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;
}
}
} 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;
}
/* 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) {
(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;
}
}
}
/* 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++;
* 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) {
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);
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]++;
}
}
}
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 {
}
/* 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;
}
}
}
}
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;
}
}
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;
/* 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 {
}
}
- 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;