#include "siphash24.h"
#include "hashmap.h"
-#include "event-util.h"
#include "lldp-tlv.h"
#include "lldp-port.h"
#include "sd-lldp.h"
#include "prioq.h"
#include "lldp-internal.h"
+#include "lldp-util.h"
+
+typedef enum LLDPAgentRXState {
+ LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL = 4,
+ LLDP_AGENT_RX_DELETE_AGED_INFO,
+ LLDP_AGENT_RX_LLDP_INITIALIZE,
+ LLDP_AGENT_RX_WAIT_FOR_FRAME,
+ LLDP_AGENT_RX_RX_FRAME,
+ LLDP_AGENT_RX_DELETE_INFO,
+ LLDP_AGENT_RX_UPDATE_INFO,
+ _LLDP_AGENT_RX_STATE_MAX,
+ _LLDP_AGENT_RX_INVALID = -1,
+} LLDPAgentRXState;
/* Section 10.5.2.2 Reception counters */
-struct lldp_agent_statitics {
+struct lldp_agent_statistics {
uint64_t stats_ageouts_total;
uint64_t stats_frames_discarded_total;
uint64_t stats_frames_in_errors_total;
Prioq *by_expiry;
Hashmap *neighbour_mib;
- lldp_agent_statitics statitics;
+ sd_lldp_cb_t cb;
+
+ void *userdata;
+
+ LLDPAgentRXState rx_state;
+ lldp_agent_statistics statistics;
};
static unsigned long chassis_id_hash_func(const void *p,
};
static void lldp_mib_delete_objects(sd_lldp *lldp);
+static void lldp_set_state(sd_lldp *lldp, LLDPAgentRXState state);
+static void lldp_run_state_machine(sd_lldp *ll);
static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
int r;
assert(tlv);
/* Remove expired packets */
- if (prioq_size(lldp->by_expiry) > 0)
+ if (prioq_size(lldp->by_expiry) > 0) {
+
+ lldp_set_state(lldp, LLDP_AGENT_RX_DELETE_INFO);
+
lldp_mib_delete_objects(lldp);
+ }
r = lldp_mib_add_objects(lldp->by_expiry, lldp->neighbour_mib, tlv);
if (r < 0)
goto out;
+ lldp_set_state(lldp, LLDP_AGENT_RX_UPDATE_INFO);
+
log_lldp("Packet added. MIB size: %d , PQ size: %d",
hashmap_size(lldp->neighbour_mib),
prioq_size(lldp->by_expiry));
- lldp->statitics.stats_frames_in_total ++;
+ lldp->statistics.stats_frames_in_total ++;
return 0;
if (r < 0)
log_lldp("Receive frame failed: %s", strerror(-r));
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
return 0;
}
goto out;
}
+ lldp_set_state(lldp, LLDP_AGENT_RX_RX_FRAME);
+
p = tlv->pdu;
p += sizeof(struct ether_header);
return lldp_receive_frame(lldp, tlv);
out:
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
if (malformed) {
- lldp->statitics.stats_frames_discarded_total ++;
- lldp->statitics.stats_frames_in_errors_total ++;
+ lldp->statistics.stats_frames_discarded_total ++;
+ lldp->statistics.stats_frames_in_errors_total ++;
}
tlv_packet_free(tlv);
return 0;
}
+static void lldp_set_state(sd_lldp *lldp, LLDPAgentRXState state) {
+
+ assert(lldp);
+ assert(state < _LLDP_AGENT_RX_STATE_MAX);
+
+ lldp->rx_state = state;
+
+ lldp_run_state_machine(lldp);
+}
+
+static void lldp_run_state_machine(sd_lldp *lldp) {
+
+ if (lldp->rx_state == LLDP_AGENT_RX_UPDATE_INFO)
+ if (lldp->cb)
+ lldp->cb(lldp, LLDP_AGENT_RX_UPDATE_INFO, lldp->userdata);
+}
+
/* 10.5.5.2.1 mibDeleteObjects ()
* The mibDeleteObjects () procedure deletes all information in the LLDP remote
* systems MIB associated with the MSAP identifier if an LLDPDU is received with
lldp_neighbour_port_remove_and_free(p);
- lldp->statitics.stats_ageouts_total ++;
+ lldp->statistics.stats_ageouts_total ++;
}
}
assert(prioq_size(lldp->by_expiry) == 0);
}
+int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
+ _cleanup_free_ char *temp_path = NULL;
+ _cleanup_fclose_ FILE *f = NULL;
+ uint8_t *mac, *port_id, type;
+ lldp_neighbour_port *p;
+ uint16_t data = 0, length = 0;
+ char buf[LINE_MAX];
+ lldp_chassis *c;
+ usec_t time;
+ Iterator i;
+ int r;
+
+ assert(lldp);
+ assert(lldp_file);
+
+ r = fopen_temporary(lldp_file, &f, &temp_path);
+ if (r < 0)
+ goto finish;
+
+ fchmod(fileno(f), 0644);
+
+ HASHMAP_FOREACH(c, lldp->neighbour_mib, i) {
+ LIST_FOREACH(port, p, c->ports) {
+ _cleanup_free_ char *s = NULL;
+ char *k, *t;
+
+ r = lldp_read_chassis_id(p->packet, &type, &length, &mac);
+ if (r < 0)
+ continue;
+
+ sprintf(buf, "'_Chassis=%02x:%02x:%02x:%02x:%02x:%02x' '_CType=%d' ",
+ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
+
+ s = strdup(buf);
+ if (!s)
+ return -ENOMEM;
+
+ r = lldp_read_port_id(p->packet, &type, &length, &port_id);
+ if (r < 0)
+ continue;
+
+ if (type != LLDP_PORT_SUBTYPE_MAC_ADDRESS) {
+ k = strndup((char *) port_id, length -1);
+ if (!k)
+ return -ENOMEM;
+
+ sprintf(buf, "'_Port=%s' '_PType=%d' ", k , type);
+ free(k);
+ } else {
+ mac = port_id;
+ sprintf(buf, "'_Port=%02x:%02x:%02x:%02x:%02x:%02x' '_PType=%d' ",
+ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
+ }
+
+ k = strappend(s, buf);
+ if (!k)
+ return -ENOMEM;
+
+ free(s);
+ s = k;
+
+ time = now(CLOCK_BOOTTIME);
+
+ /* Don't write expired packets */
+ if (time - p->until <= 0)
+ continue;
+
+ sprintf(buf, "'_TTL="USEC_FMT"' ", p->until);
+
+ k = strappend(s, buf);
+ if (!k)
+ return -ENOMEM;
+
+ free(s);
+ s = k;
+
+ r = lldp_read_system_name(p->packet, &length, &k);
+ if (r < 0)
+ k = strappend(s, "'_NAME=N/A' ");
+ else {
+ t = strndup(k, length);
+ if (!t)
+ return -ENOMEM;
+
+ k = strjoin(s, "'_NAME=", t, "' ", NULL);
+ free(t);
+ }
+
+ if (!k)
+ return -ENOMEM;
+
+ free(s);
+ s = k;
+
+ (void)lldp_read_system_capability(p->packet, &data);
+
+ sprintf(buf, "'_CAP=%x'", data);
+
+ k = strappend(s, buf);
+ if (!k)
+ return -ENOMEM;
+
+ free(s);
+ s = k;
+
+ fprintf(f, "%s\n", s);
+ }
+ }
+ r = 0;
+
+ fflush(f);
+
+ if (ferror(f) || rename(temp_path, lldp_file) < 0) {
+ r = -errno;
+ unlink(lldp_file);
+ unlink(temp_path);
+ }
+
+ finish:
+ if (r < 0)
+ log_error("Failed to save lldp data %s: %s", lldp_file, strerror(-r));
+
+ return r;
+}
+
int sd_lldp_start(sd_lldp *lldp) {
int r;
lldp->port->status = LLDP_PORT_STATUS_ENABLED;
+ lldp_set_state(lldp, LLDP_AGENT_RX_LLDP_INITIALIZE);
+
r = lldp_port_start(lldp->port);
if (r < 0) {
log_lldp("Failed to start Port : %s , %s",
lldp->port->ifname,
strerror(-r));
+
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL);
+
return r;
}
+ lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
return 0;
}
return 0;
}
+int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata) {
+ assert_return(lldp, -EINVAL);
+
+ lldp->cb = cb;
+ lldp->userdata = userdata;
+
+ return 0;
+}
+
void sd_lldp_free(sd_lldp *lldp) {
if (!lldp)
}
int sd_lldp_new(int ifindex,
- char *ifname,
- struct ether_addr *mac,
+ const char *ifname,
+ const struct ether_addr *mac,
sd_lldp **ret) {
- _cleanup_sd_lldp_free_ sd_lldp *lldp = NULL;
+ _cleanup_lldp_free_ sd_lldp *lldp = NULL;
int r;
assert_return(ret, -EINVAL);
if (r < 0)
return r;
+ lldp->rx_state = LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL;
+
*ret = lldp;
lldp = NULL;