1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-03 01:17:45 +03:00

sd-lldp-rx: rename sd_lldp -> sd_lldp_rx

This commit is contained in:
Yu Watanabe 2021-09-26 12:39:36 +09:00
parent 94832e6e55
commit 35778343ab
11 changed files with 312 additions and 312 deletions

View File

@ -21,22 +21,22 @@ int lldp_network_bind_raw_socket(int ifindex) {
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_(sd_lldp_unrefp) sd_lldp *lldp = NULL;
_cleanup_(sd_lldp_rx_unrefp) sd_lldp_rx *lldp_rx = NULL;
if (size > 2048)
return 0;
assert_se(sd_event_new(&e) == 0);
assert_se(sd_lldp_new(&lldp) >= 0);
assert_se(sd_lldp_set_ifindex(lldp, 42) >= 0);
assert_se(sd_lldp_attach_event(lldp, e, 0) >= 0);
assert_se(sd_lldp_start(lldp) >= 0);
assert_se(sd_lldp_rx_new(&lldp_rx) >= 0);
assert_se(sd_lldp_rx_set_ifindex(lldp_rx, 42) >= 0);
assert_se(sd_lldp_rx_attach_event(lldp_rx, e, 0) >= 0);
assert_se(sd_lldp_rx_start(lldp_rx) >= 0);
assert_se(write(test_fd[1], data, size) == (ssize_t) size);
assert_se(sd_event_run(e, 0) >= 0);
assert_se(sd_lldp_stop(lldp) >= 0);
assert_se(sd_lldp_detach_event(lldp) >= 0);
assert_se(sd_lldp_rx_stop(lldp_rx) >= 0);
assert_se(sd_lldp_rx_detach_event(lldp_rx) >= 0);
test_fd[1] = safe_close(test_fd[1]);
return 0;

View File

@ -35,7 +35,7 @@ _public_ sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n) {
if (!n)
return NULL;
assert(n->n_ref > 0 || n->lldp);
assert(n->n_ref > 0 || n->lldp_rx);
n->n_ref++;
return n;
@ -66,7 +66,7 @@ _public_ sd_lldp_neighbor *sd_lldp_neighbor_unref(sd_lldp_neighbor *n) {
assert(n->n_ref > 0);
n->n_ref--;
if (n->n_ref <= 0 && !n->lldp)
if (n->n_ref <= 0 && !n->lldp_rx)
lldp_neighbor_free(n);
return NULL;
@ -79,18 +79,18 @@ sd_lldp_neighbor *lldp_neighbor_unlink(sd_lldp_neighbor *n) {
if (!n)
return NULL;
if (!n->lldp)
if (!n->lldp_rx)
return NULL;
/* Only remove the neighbor object from the hash table if it's in there, don't complain if it isn't. This is
* because we are used as destructor call for hashmap_clear() and thus sometimes are called to de-register
* ourselves from the hashtable and sometimes are called after we already are de-registered. */
(void) hashmap_remove_value(n->lldp->neighbor_by_id, &n->id, n);
(void) hashmap_remove_value(n->lldp_rx->neighbor_by_id, &n->id, n);
assert_se(prioq_remove(n->lldp->neighbor_by_expiry, n, &n->prioq_idx) >= 0);
assert_se(prioq_remove(n->lldp_rx->neighbor_by_expiry, n, &n->prioq_idx) >= 0);
n->lldp = NULL;
n->lldp_rx = NULL;
if (n->n_ref <= 0)
lldp_neighbor_free(n);
@ -111,7 +111,7 @@ sd_lldp_neighbor *lldp_neighbor_new(size_t raw_size) {
return n;
}
static int parse_string(sd_lldp *lldp, char **s, const void *q, size_t n) {
static int parse_string(sd_lldp_rx *lldp_rx, char **s, const void *q, size_t n) {
const char *p = q;
char *k;
@ -119,7 +119,7 @@ static int parse_string(sd_lldp *lldp, char **s, const void *q, size_t n) {
assert(p || n == 0);
if (*s) {
log_lldp(lldp, "Found duplicate string, ignoring field.");
log_lldp_rx(lldp_rx, "Found duplicate string, ignoring field.");
return 0;
}
@ -132,7 +132,7 @@ static int parse_string(sd_lldp *lldp, char **s, const void *q, size_t n) {
/* Look for inner NULs */
if (memchr(p, 0, n)) {
log_lldp(lldp, "Found inner NUL in string, ignoring field.");
log_lldp_rx(lldp_rx, "Found inner NUL in string, ignoring field.");
return 0;
}
@ -156,14 +156,14 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
assert(n);
if (n->raw_size < sizeof(struct ether_header))
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Received truncated packet, ignoring.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Received truncated packet, ignoring.");
memcpy(&h, LLDP_NEIGHBOR_RAW(n), sizeof(h));
if (h.ether_type != htobe16(ETHERTYPE_LLDP))
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Received packet with wrong type, ignoring.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Received packet with wrong type, ignoring.");
if (h.ether_dhost[0] != 0x01 ||
h.ether_dhost[1] != 0x80 ||
@ -171,8 +171,8 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
h.ether_dhost[3] != 0x00 ||
h.ether_dhost[4] != 0x00 ||
!IN_SET(h.ether_dhost[5], 0x00, 0x03, 0x0e))
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Received packet with wrong destination address, ignoring.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Received packet with wrong destination address, ignoring.");
memcpy(&n->source_address, h.ether_shost, sizeof(struct ether_addr));
memcpy(&n->destination_address, h.ether_dhost, sizeof(struct ether_addr));
@ -185,7 +185,7 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
uint16_t length;
if (left < 2)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"TLV lacks header, ignoring.");
type = p[0] >> 1;
@ -193,15 +193,15 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
p += 2, left -= 2;
if (left < length)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"TLV truncated, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"TLV truncated, ignoring datagram.");
switch (type) {
case SD_LLDP_TYPE_END:
if (length != 0)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"End marker TLV not zero-sized, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"End marker TLV not zero-sized, ignoring datagram.");
/* Note that after processing the SD_LLDP_TYPE_END left could still be > 0
* as the message may contain padding (see IEEE 802.1AB-2016, sec. 8.5.12) */
@ -211,12 +211,12 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
case SD_LLDP_TYPE_CHASSIS_ID:
if (length < 2 || length > 256)
/* includes the chassis subtype, hence one extra byte */
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Chassis ID field size out of range, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Chassis ID field size out of range, ignoring datagram.");
if (n->id.chassis_id)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate chassis ID field, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate chassis ID field, ignoring datagram.");
n->id.chassis_id = memdup(p, length);
if (!n->id.chassis_id)
@ -228,12 +228,12 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
case SD_LLDP_TYPE_PORT_ID:
if (length < 2 || length > 256)
/* includes the port subtype, hence one extra byte */
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Port ID field size out of range, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Port ID field size out of range, ignoring datagram.");
if (n->id.port_id)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate port ID field, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate port ID field, ignoring datagram.");
n->id.port_id = memdup(p, length);
if (!n->id.port_id)
@ -244,39 +244,39 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
case SD_LLDP_TYPE_TTL:
if (length != 2)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"TTL field has wrong size, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"TTL field has wrong size, ignoring datagram.");
if (n->has_ttl)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate TTL field, ignoring datagram.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Duplicate TTL field, ignoring datagram.");
n->ttl = unaligned_read_be16(p);
n->has_ttl = true;
break;
case SD_LLDP_TYPE_PORT_DESCRIPTION:
r = parse_string(n->lldp, &n->port_description, p, length);
r = parse_string(n->lldp_rx, &n->port_description, p, length);
if (r < 0)
return r;
break;
case SD_LLDP_TYPE_SYSTEM_NAME:
r = parse_string(n->lldp, &n->system_name, p, length);
r = parse_string(n->lldp_rx, &n->system_name, p, length);
if (r < 0)
return r;
break;
case SD_LLDP_TYPE_SYSTEM_DESCRIPTION:
r = parse_string(n->lldp, &n->system_description, p, length);
r = parse_string(n->lldp_rx, &n->system_description, p, length);
if (r < 0)
return r;
break;
case SD_LLDP_TYPE_SYSTEM_CAPABILITIES:
if (length != 4)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"System capabilities field has wrong size.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"System capabilities field has wrong size.");
n->system_capabilities = unaligned_read_be16(p);
n->enabled_capabilities = unaligned_read_be16(p + 2);
@ -285,13 +285,13 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
case SD_LLDP_TYPE_PRIVATE:
if (length < 4)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"Found private TLV that is too short, ignoring.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"Found private TLV that is too short, ignoring.");
/* RFC 8520: MUD URL */
if (memcmp(p, SD_LLDP_OUI_MUD, sizeof(SD_LLDP_OUI_MUD)) == 0 &&
p[sizeof(SD_LLDP_OUI_MUD)] == SD_LLDP_OUI_SUBTYPE_MUD_USAGE_DESCRIPTION) {
r = parse_string(n->lldp, &n->mud_url, p + sizeof(SD_LLDP_OUI_MUD) + 1,
r = parse_string(n->lldp_rx, &n->mud_url, p + sizeof(SD_LLDP_OUI_MUD) + 1,
length - 1 - sizeof(SD_LLDP_OUI_MUD));
if (r < 0)
return r;
@ -304,8 +304,8 @@ int lldp_neighbor_parse(sd_lldp_neighbor *n) {
end_marker:
if (!n->id.chassis_id || !n->id.port_id || !n->has_ttl)
return log_lldp_errno(n->lldp, SYNTHETIC_ERRNO(EBADMSG),
"One or more mandatory TLV missing in datagram. Ignoring.");
return log_lldp_rx_errno(n->lldp_rx, SYNTHETIC_ERRNO(EBADMSG),
"One or more mandatory TLV missing in datagram. Ignoring.");
n->rindex = sizeof(struct ether_header);
@ -327,8 +327,8 @@ void lldp_neighbor_start_ttl(sd_lldp_neighbor *n) {
} else
n->until = 0;
if (n->lldp)
prioq_reshuffle(n->lldp->neighbor_by_expiry, n, &n->prioq_idx);
if (n->lldp_rx)
prioq_reshuffle(n->lldp_rx->neighbor_by_expiry, n, &n->prioq_idx);
}
bool lldp_neighbor_equal(const sd_lldp_neighbor *a, const sd_lldp_neighbor *b) {

View File

@ -21,8 +21,8 @@ typedef struct LLDPNeighborID {
} LLDPNeighborID;
struct sd_lldp_neighbor {
/* Neighbor objects stay around as long as they are linked into an "sd_lldp" object or n_ref > 0. */
sd_lldp *lldp;
/* Neighbor objects stay around as long as they are linked into an "sd_lldp_rx" object or n_ref > 0. */
sd_lldp_rx *lldp_rx;
unsigned n_ref;
triple_timestamp timestamp;

View File

@ -8,7 +8,7 @@
#include "log-link.h"
#include "prioq.h"
struct sd_lldp {
struct sd_lldp_rx {
unsigned n_ref;
int ifindex;
@ -25,7 +25,7 @@ struct sd_lldp {
uint64_t neighbors_max;
sd_lldp_callback_t callback;
sd_lldp_rx_callback_t callback;
void *userdata;
uint16_t capability_mask;
@ -33,16 +33,16 @@ struct sd_lldp {
struct ether_addr filter_address;
};
const char* lldp_event_to_string(sd_lldp_event_t e) _const_;
sd_lldp_event_t lldp_event_from_string(const char *s) _pure_;
const char* lldp_rx_event_to_string(sd_lldp_rx_event_t e) _const_;
sd_lldp_rx_event_t lldp_rx_event_from_string(const char *s) _pure_;
#define log_lldp_errno(lldp, error, fmt, ...) \
#define log_lldp_rx_errno(lldp_rx, error, fmt, ...) \
log_interface_prefix_full_errno( \
"LLDP: ", \
sd_lldp_get_ifname(lldp), \
"LLDP Rx: ", \
sd_lldp_rx_get_ifname(lldp_rx), \
error, fmt, ##__VA_ARGS__)
#define log_lldp(lldp, fmt, ...) \
#define log_lldp_rx(lldp_rx, fmt, ...) \
log_interface_prefix_full_errno_zerook( \
"LLDP: ", \
sd_lldp_get_ifname(lldp), \
"LLDP Rx: ", \
sd_lldp_rx_get_ifname(lldp_rx), \
0, fmt, ##__VA_ARGS__)

View File

@ -21,37 +21,37 @@
#define LLDP_DEFAULT_NEIGHBORS_MAX 128U
static const char * const lldp_event_table[_SD_LLDP_EVENT_MAX] = {
[SD_LLDP_EVENT_ADDED] = "added",
[SD_LLDP_EVENT_REMOVED] = "removed",
[SD_LLDP_EVENT_UPDATED] = "updated",
[SD_LLDP_EVENT_REFRESHED] = "refreshed",
static const char * const lldp_rx_event_table[_SD_LLDP_RX_EVENT_MAX] = {
[SD_LLDP_RX_EVENT_ADDED] = "added",
[SD_LLDP_RX_EVENT_REMOVED] = "removed",
[SD_LLDP_RX_EVENT_UPDATED] = "updated",
[SD_LLDP_RX_EVENT_REFRESHED] = "refreshed",
};
DEFINE_STRING_TABLE_LOOKUP(lldp_event, sd_lldp_event_t);
DEFINE_STRING_TABLE_LOOKUP(lldp_rx_event, sd_lldp_rx_event_t);
static void lldp_flush_neighbors(sd_lldp *lldp) {
assert(lldp);
static void lldp_rx_flush_neighbors(sd_lldp_rx *lldp_rx) {
assert(lldp_rx);
hashmap_clear(lldp->neighbor_by_id);
hashmap_clear(lldp_rx->neighbor_by_id);
}
static void lldp_callback(sd_lldp *lldp, sd_lldp_event_t event, sd_lldp_neighbor *n) {
assert(lldp);
assert(event >= 0 && event < _SD_LLDP_EVENT_MAX);
static void lldp_rx_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n) {
assert(lldp_rx);
assert(event >= 0 && event < _SD_LLDP_RX_EVENT_MAX);
if (!lldp->callback)
return (void) log_lldp(lldp, "Received '%s' event.", lldp_event_to_string(event));
if (!lldp_rx->callback)
return (void) log_lldp_rx(lldp_rx, "Received '%s' event.", lldp_rx_event_to_string(event));
log_lldp(lldp, "Invoking callback for '%s' event.", lldp_event_to_string(event));
lldp->callback(lldp, event, n, lldp->userdata);
log_lldp_rx(lldp_rx, "Invoking callback for '%s' event.", lldp_rx_event_to_string(event));
lldp_rx->callback(lldp_rx, event, n, lldp_rx->userdata);
}
static int lldp_make_space(sd_lldp *lldp, size_t extra) {
static int lldp_rx_make_space(sd_lldp_rx *lldp_rx, size_t extra) {
usec_t t = USEC_INFINITY;
bool changed = false;
assert(lldp);
assert(lldp_rx);
/* Remove all entries that are past their TTL, and more until at least the specified number of extra entries
* are free. */
@ -59,13 +59,13 @@ static int lldp_make_space(sd_lldp *lldp, size_t extra) {
for (;;) {
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
n = prioq_peek(lldp->neighbor_by_expiry);
n = prioq_peek(lldp_rx->neighbor_by_expiry);
if (!n)
break;
sd_lldp_neighbor_ref(n);
if (hashmap_size(lldp->neighbor_by_id) > LESS_BY(lldp->neighbors_max, extra))
if (hashmap_size(lldp_rx->neighbor_by_id) > LESS_BY(lldp_rx->neighbors_max, extra))
goto remove_one;
if (t == USEC_INFINITY)
@ -76,15 +76,15 @@ static int lldp_make_space(sd_lldp *lldp, size_t extra) {
remove_one:
lldp_neighbor_unlink(n);
lldp_callback(lldp, SD_LLDP_EVENT_REMOVED, n);
lldp_rx_callback(lldp_rx, SD_LLDP_RX_EVENT_REMOVED, n);
changed = true;
}
return changed;
}
static bool lldp_keep_neighbor(sd_lldp *lldp, sd_lldp_neighbor *n) {
assert(lldp);
static bool lldp_rx_keep_neighbor(sd_lldp_rx *lldp_rx, sd_lldp_neighbor *n) {
assert(lldp_rx);
assert(n);
/* Don't keep data with a zero TTL */
@ -92,49 +92,49 @@ static bool lldp_keep_neighbor(sd_lldp *lldp, sd_lldp_neighbor *n) {
return false;
/* Filter out data from the filter address */
if (!ether_addr_is_null(&lldp->filter_address) &&
ether_addr_equal(&lldp->filter_address, &n->source_address))
if (!ether_addr_is_null(&lldp_rx->filter_address) &&
ether_addr_equal(&lldp_rx->filter_address, &n->source_address))
return false;
/* Only add if the neighbor has a capability we are interested in. Note that we also store all neighbors with
* no caps field set. */
if (n->has_capabilities &&
(n->enabled_capabilities & lldp->capability_mask) == 0)
(n->enabled_capabilities & lldp_rx->capability_mask) == 0)
return false;
/* Keep everything else */
return true;
}
static int lldp_start_timer(sd_lldp *lldp, sd_lldp_neighbor *neighbor);
static int lldp_rx_start_timer(sd_lldp_rx *lldp_rx, sd_lldp_neighbor *neighbor);
static int lldp_add_neighbor(sd_lldp *lldp, sd_lldp_neighbor *n) {
static int lldp_rx_add_neighbor(sd_lldp_rx *lldp_rx, sd_lldp_neighbor *n) {
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *old = NULL;
bool keep;
int r;
assert(lldp);
assert(lldp_rx);
assert(n);
assert(!n->lldp);
assert(!n->lldp_rx);
keep = lldp_keep_neighbor(lldp, n);
keep = lldp_rx_keep_neighbor(lldp_rx, n);
/* First retrieve the old entry for this MSAP */
old = hashmap_get(lldp->neighbor_by_id, &n->id);
old = hashmap_get(lldp_rx->neighbor_by_id, &n->id);
if (old) {
sd_lldp_neighbor_ref(old);
if (!keep) {
lldp_neighbor_unlink(old);
lldp_callback(lldp, SD_LLDP_EVENT_REMOVED, old);
lldp_rx_callback(lldp_rx, SD_LLDP_RX_EVENT_REMOVED, old);
return 0;
}
if (lldp_neighbor_equal(n, old)) {
/* Is this equal, then restart the TTL counter, but don't do anything else. */
old->timestamp = n->timestamp;
lldp_start_timer(lldp, old);
lldp_callback(lldp, SD_LLDP_EVENT_REFRESHED, old);
lldp_rx_start_timer(lldp_rx, old);
lldp_rx_callback(lldp_rx, SD_LLDP_RX_EVENT_REFRESHED, old);
return 0;
}
@ -145,36 +145,36 @@ static int lldp_add_neighbor(sd_lldp *lldp, sd_lldp_neighbor *n) {
return 0;
/* Then, make room for at least one new neighbor */
lldp_make_space(lldp, 1);
lldp_rx_make_space(lldp_rx, 1);
r = hashmap_put(lldp->neighbor_by_id, &n->id, n);
r = hashmap_put(lldp_rx->neighbor_by_id, &n->id, n);
if (r < 0)
goto finish;
r = prioq_put(lldp->neighbor_by_expiry, n, &n->prioq_idx);
r = prioq_put(lldp_rx->neighbor_by_expiry, n, &n->prioq_idx);
if (r < 0) {
assert_se(hashmap_remove(lldp->neighbor_by_id, &n->id) == n);
assert_se(hashmap_remove(lldp_rx->neighbor_by_id, &n->id) == n);
goto finish;
}
n->lldp = lldp;
n->lldp_rx = lldp_rx;
lldp_start_timer(lldp, n);
lldp_callback(lldp, old ? SD_LLDP_EVENT_UPDATED : SD_LLDP_EVENT_ADDED, n);
lldp_rx_start_timer(lldp_rx, n);
lldp_rx_callback(lldp_rx, old ? SD_LLDP_RX_EVENT_UPDATED : SD_LLDP_RX_EVENT_ADDED, n);
return 1;
finish:
if (old)
lldp_callback(lldp, SD_LLDP_EVENT_REMOVED, old);
lldp_rx_callback(lldp_rx, SD_LLDP_RX_EVENT_REMOVED, old);
return r;
}
static int lldp_handle_datagram(sd_lldp *lldp, sd_lldp_neighbor *n) {
static int lldp_rx_handle_datagram(sd_lldp_rx *lldp_rx, sd_lldp_neighbor *n) {
int r;
assert(lldp);
assert(lldp_rx);
assert(n);
r = lldp_neighbor_parse(n);
@ -183,28 +183,28 @@ static int lldp_handle_datagram(sd_lldp *lldp, sd_lldp_neighbor *n) {
if (r < 0)
return r;
r = lldp_add_neighbor(lldp, n);
r = lldp_rx_add_neighbor(lldp_rx, n);
if (r < 0) {
log_lldp_errno(lldp, r, "Failed to add datagram. Ignoring.");
log_lldp_rx_errno(lldp_rx, r, "Failed to add datagram. Ignoring.");
return 0;
}
log_lldp(lldp, "Successfully processed LLDP datagram.");
log_lldp_rx(lldp_rx, "Successfully processed LLDP datagram.");
return 0;
}
static int lldp_receive_datagram(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
static int lldp_rx_receive_datagram(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
ssize_t space, length;
sd_lldp *lldp = userdata;
sd_lldp_rx *lldp_rx = userdata;
struct timespec ts;
assert(fd >= 0);
assert(lldp);
assert(lldp_rx);
space = next_datagram_size_fd(fd);
if (space < 0) {
log_lldp_errno(lldp, space, "Failed to determine datagram size to read, ignoring: %m");
log_lldp_rx_errno(lldp_rx, space, "Failed to determine datagram size to read, ignoring: %m");
return 0;
}
@ -217,12 +217,12 @@ static int lldp_receive_datagram(sd_event_source *s, int fd, uint32_t revents, v
if (IN_SET(errno, EAGAIN, EINTR))
return 0;
log_lldp_errno(lldp, errno, "Failed to read LLDP datagram, ignoring: %m");
log_lldp_rx_errno(lldp_rx, errno, "Failed to read LLDP datagram, ignoring: %m");
return 0;
}
if ((size_t) length != n->raw_size) {
log_lldp(lldp, "Packet size mismatch, ignoring");
log_lldp_rx(lldp_rx, "Packet size mismatch, ignoring");
return 0;
}
@ -232,180 +232,180 @@ static int lldp_receive_datagram(sd_event_source *s, int fd, uint32_t revents, v
else
triple_timestamp_get(&n->timestamp);
return lldp_handle_datagram(lldp, n);
return lldp_rx_handle_datagram(lldp_rx, n);
}
static void lldp_reset(sd_lldp *lldp) {
assert(lldp);
static void lldp_rx_reset(sd_lldp_rx *lldp_rx) {
assert(lldp_rx);
(void) event_source_disable(lldp->timer_event_source);
lldp->io_event_source = sd_event_source_disable_unref(lldp->io_event_source);
lldp->fd = safe_close(lldp->fd);
(void) event_source_disable(lldp_rx->timer_event_source);
lldp_rx->io_event_source = sd_event_source_disable_unref(lldp_rx->io_event_source);
lldp_rx->fd = safe_close(lldp_rx->fd);
}
_public_ int sd_lldp_start(sd_lldp *lldp) {
_public_ int sd_lldp_rx_start(sd_lldp_rx *lldp_rx) {
int r;
assert_return(lldp, -EINVAL);
assert_return(lldp->event, -EINVAL);
assert_return(lldp->ifindex > 0, -EINVAL);
assert_return(lldp_rx, -EINVAL);
assert_return(lldp_rx->event, -EINVAL);
assert_return(lldp_rx->ifindex > 0, -EINVAL);
if (lldp->fd >= 0)
if (lldp_rx->fd >= 0)
return 0;
assert(!lldp->io_event_source);
assert(!lldp_rx->io_event_source);
lldp->fd = lldp_network_bind_raw_socket(lldp->ifindex);
if (lldp->fd < 0)
return lldp->fd;
lldp_rx->fd = lldp_network_bind_raw_socket(lldp_rx->ifindex);
if (lldp_rx->fd < 0)
return lldp_rx->fd;
r = sd_event_add_io(lldp->event, &lldp->io_event_source, lldp->fd, EPOLLIN, lldp_receive_datagram, lldp);
r = sd_event_add_io(lldp_rx->event, &lldp_rx->io_event_source, lldp_rx->fd, EPOLLIN, lldp_rx_receive_datagram, lldp_rx);
if (r < 0)
goto fail;
r = sd_event_source_set_priority(lldp->io_event_source, lldp->event_priority);
r = sd_event_source_set_priority(lldp_rx->io_event_source, lldp_rx->event_priority);
if (r < 0)
goto fail;
(void) sd_event_source_set_description(lldp->io_event_source, "lldp-io");
(void) sd_event_source_set_description(lldp_rx->io_event_source, "lldp-rx-io");
log_lldp(lldp, "Started LLDP client");
log_lldp_rx(lldp_rx, "Started LLDP client");
return 1;
fail:
lldp_reset(lldp);
lldp_rx_reset(lldp_rx);
return r;
}
_public_ int sd_lldp_stop(sd_lldp *lldp) {
if (!lldp)
_public_ int sd_lldp_rx_stop(sd_lldp_rx *lldp_rx) {
if (!lldp_rx)
return 0;
if (lldp->fd < 0)
if (lldp_rx->fd < 0)
return 0;
log_lldp(lldp, "Stopping LLDP client");
log_lldp_rx(lldp_rx, "Stopping LLDP client");
lldp_reset(lldp);
lldp_flush_neighbors(lldp);
lldp_rx_reset(lldp_rx);
lldp_rx_flush_neighbors(lldp_rx);
return 1;
}
_public_ int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int64_t priority) {
_public_ int sd_lldp_rx_attach_event(sd_lldp_rx *lldp_rx, sd_event *event, int64_t priority) {
int r;
assert_return(lldp, -EINVAL);
assert_return(lldp->fd < 0, -EBUSY);
assert_return(!lldp->event, -EBUSY);
assert_return(lldp_rx, -EINVAL);
assert_return(lldp_rx->fd < 0, -EBUSY);
assert_return(!lldp_rx->event, -EBUSY);
if (event)
lldp->event = sd_event_ref(event);
lldp_rx->event = sd_event_ref(event);
else {
r = sd_event_default(&lldp->event);
r = sd_event_default(&lldp_rx->event);
if (r < 0)
return r;
}
lldp->event_priority = priority;
lldp_rx->event_priority = priority;
return 0;
}
_public_ int sd_lldp_detach_event(sd_lldp *lldp) {
_public_ int sd_lldp_rx_detach_event(sd_lldp_rx *lldp_rx) {
assert_return(lldp, -EINVAL);
assert_return(lldp->fd < 0, -EBUSY);
assert_return(lldp_rx, -EINVAL);
assert_return(lldp_rx->fd < 0, -EBUSY);
lldp->event = sd_event_unref(lldp->event);
lldp_rx->event = sd_event_unref(lldp_rx->event);
return 0;
}
_public_ sd_event* sd_lldp_get_event(sd_lldp *lldp) {
assert_return(lldp, NULL);
_public_ sd_event* sd_lldp_rx_get_event(sd_lldp_rx *lldp_rx) {
assert_return(lldp_rx, NULL);
return lldp->event;
return lldp_rx->event;
}
_public_ int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_callback_t cb, void *userdata) {
assert_return(lldp, -EINVAL);
_public_ int sd_lldp_rx_set_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_callback_t cb, void *userdata) {
assert_return(lldp_rx, -EINVAL);
lldp->callback = cb;
lldp->userdata = userdata;
lldp_rx->callback = cb;
lldp_rx->userdata = userdata;
return 0;
}
_public_ int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex) {
assert_return(lldp, -EINVAL);
_public_ int sd_lldp_rx_set_ifindex(sd_lldp_rx *lldp_rx, int ifindex) {
assert_return(lldp_rx, -EINVAL);
assert_return(ifindex > 0, -EINVAL);
assert_return(lldp->fd < 0, -EBUSY);
assert_return(lldp_rx->fd < 0, -EBUSY);
lldp->ifindex = ifindex;
lldp_rx->ifindex = ifindex;
return 0;
}
int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname) {
assert_return(lldp, -EINVAL);
int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname) {
assert_return(lldp_rx, -EINVAL);
assert_return(ifname, -EINVAL);
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
return -EINVAL;
return free_and_strdup(&lldp->ifname, ifname);
return free_and_strdup(&lldp_rx->ifname, ifname);
}
const char *sd_lldp_get_ifname(sd_lldp *lldp) {
if (!lldp)
const char *sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx) {
if (!lldp_rx)
return NULL;
return get_ifname(lldp->ifindex, &lldp->ifname);
return get_ifname(lldp_rx->ifindex, &lldp_rx->ifname);
}
static sd_lldp* lldp_free(sd_lldp *lldp) {
assert(lldp);
static sd_lldp_rx *lldp_rx_free(sd_lldp_rx *lldp_rx) {
assert(lldp_rx);
lldp_reset(lldp);
lldp_rx_reset(lldp_rx);
sd_event_source_unref(lldp->timer_event_source);
sd_lldp_detach_event(lldp);
sd_event_source_unref(lldp_rx->timer_event_source);
sd_lldp_rx_detach_event(lldp_rx);
lldp_flush_neighbors(lldp);
lldp_rx_flush_neighbors(lldp_rx);
hashmap_free(lldp->neighbor_by_id);
prioq_free(lldp->neighbor_by_expiry);
free(lldp->ifname);
return mfree(lldp);
hashmap_free(lldp_rx->neighbor_by_id);
prioq_free(lldp_rx->neighbor_by_expiry);
free(lldp_rx->ifname);
return mfree(lldp_rx);
}
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_lldp, sd_lldp, lldp_free);
DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_lldp_rx, sd_lldp_rx, lldp_rx_free);
_public_ int sd_lldp_new(sd_lldp **ret) {
_cleanup_(sd_lldp_unrefp) sd_lldp *lldp = NULL;
_public_ int sd_lldp_rx_new(sd_lldp_rx **ret) {
_cleanup_(sd_lldp_rx_unrefp) sd_lldp_rx *lldp_rx = NULL;
int r;
assert_return(ret, -EINVAL);
lldp = new(sd_lldp, 1);
if (!lldp)
lldp_rx = new(sd_lldp_rx, 1);
if (!lldp_rx)
return -ENOMEM;
*lldp = (sd_lldp) {
*lldp_rx = (sd_lldp_rx) {
.n_ref = 1,
.fd = -1,
.neighbors_max = LLDP_DEFAULT_NEIGHBORS_MAX,
.capability_mask = UINT16_MAX,
};
lldp->neighbor_by_id = hashmap_new(&lldp_neighbor_hash_ops);
if (!lldp->neighbor_by_id)
lldp_rx->neighbor_by_id = hashmap_new(&lldp_neighbor_hash_ops);
if (!lldp_rx->neighbor_by_id)
return -ENOMEM;
r = prioq_ensure_allocated(&lldp->neighbor_by_expiry, lldp_neighbor_prioq_compare_func);
r = prioq_ensure_allocated(&lldp_rx->neighbor_by_expiry, lldp_neighbor_prioq_compare_func);
if (r < 0)
return r;
*ret = TAKE_PTR(lldp);
*ret = TAKE_PTR(lldp_rx);
return 0;
}
@ -415,72 +415,72 @@ static int neighbor_compare_func(sd_lldp_neighbor * const *a, sd_lldp_neighbor *
}
static int on_timer_event(sd_event_source *s, uint64_t usec, void *userdata) {
sd_lldp *lldp = userdata;
sd_lldp_rx *lldp_rx = userdata;
int r;
r = lldp_make_space(lldp, 0);
r = lldp_rx_make_space(lldp_rx, 0);
if (r < 0) {
log_lldp_errno(lldp, r, "Failed to make space, ignoring: %m");
log_lldp_rx_errno(lldp_rx, r, "Failed to make space, ignoring: %m");
return 0;
}
r = lldp_start_timer(lldp, NULL);
r = lldp_rx_start_timer(lldp_rx, NULL);
if (r < 0) {
log_lldp_errno(lldp, r, "Failed to restart timer, ignoring: %m");
log_lldp_rx_errno(lldp_rx, r, "Failed to restart timer, ignoring: %m");
return 0;
}
return 0;
}
static int lldp_start_timer(sd_lldp *lldp, sd_lldp_neighbor *neighbor) {
static int lldp_rx_start_timer(sd_lldp_rx *lldp_rx, sd_lldp_neighbor *neighbor) {
sd_lldp_neighbor *n;
assert(lldp);
assert(lldp_rx);
if (neighbor)
lldp_neighbor_start_ttl(neighbor);
n = prioq_peek(lldp->neighbor_by_expiry);
n = prioq_peek(lldp_rx->neighbor_by_expiry);
if (!n)
return event_source_disable(lldp->timer_event_source);
return event_source_disable(lldp_rx->timer_event_source);
if (!lldp->event)
if (!lldp_rx->event)
return 0;
return event_reset_time(lldp->event, &lldp->timer_event_source,
return event_reset_time(lldp_rx->event, &lldp_rx->timer_event_source,
clock_boottime_or_monotonic(),
n->until, 0,
on_timer_event, lldp,
lldp->event_priority, "lldp-timer", true);
on_timer_event, lldp_rx,
lldp_rx->event_priority, "lldp-rx-timer", true);
}
_public_ int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***ret) {
_public_ int sd_lldp_rx_get_neighbors(sd_lldp_rx *lldp_rx, sd_lldp_neighbor ***ret) {
sd_lldp_neighbor **l = NULL, *n;
int k = 0, r;
assert_return(lldp, -EINVAL);
assert_return(lldp_rx, -EINVAL);
assert_return(ret, -EINVAL);
if (hashmap_isempty(lldp->neighbor_by_id)) { /* Special shortcut */
if (hashmap_isempty(lldp_rx->neighbor_by_id)) { /* Special shortcut */
*ret = NULL;
return 0;
}
l = new0(sd_lldp_neighbor*, hashmap_size(lldp->neighbor_by_id));
l = new0(sd_lldp_neighbor*, hashmap_size(lldp_rx->neighbor_by_id));
if (!l)
return -ENOMEM;
r = lldp_start_timer(lldp, NULL);
r = lldp_rx_start_timer(lldp_rx, NULL);
if (r < 0) {
free(l);
return r;
}
HASHMAP_FOREACH(n, lldp->neighbor_by_id)
HASHMAP_FOREACH(n, lldp_rx->neighbor_by_id)
l[k++] = sd_lldp_neighbor_ref(n);
assert((size_t) k == hashmap_size(lldp->neighbor_by_id));
assert((size_t) k == hashmap_size(lldp_rx->neighbor_by_id));
/* Return things in a stable order */
typesafe_qsort(l, k, neighbor_compare_func);
@ -489,35 +489,35 @@ _public_ int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***ret) {
return k;
}
_public_ int sd_lldp_set_neighbors_max(sd_lldp *lldp, uint64_t m) {
assert_return(lldp, -EINVAL);
_public_ int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t m) {
assert_return(lldp_rx, -EINVAL);
assert_return(m > 0, -EINVAL);
lldp->neighbors_max = m;
lldp_make_space(lldp, 0);
lldp_rx->neighbors_max = m;
lldp_rx_make_space(lldp_rx, 0);
return 0;
}
_public_ int sd_lldp_match_capabilities(sd_lldp *lldp, uint16_t mask) {
assert_return(lldp, -EINVAL);
_public_ int sd_lldp_rx_match_capabilities(sd_lldp_rx *lldp_rx, uint16_t mask) {
assert_return(lldp_rx, -EINVAL);
assert_return(mask != 0, -EINVAL);
lldp->capability_mask = mask;
lldp_rx->capability_mask = mask;
return 0;
}
_public_ int sd_lldp_set_filter_address(sd_lldp *lldp, const struct ether_addr *addr) {
assert_return(lldp, -EINVAL);
_public_ int sd_lldp_rx_set_filter_address(sd_lldp_rx *lldp_rx, const struct ether_addr *addr) {
assert_return(lldp_rx, -EINVAL);
/* In order to deal nicely with bridges that send back our own packets, allow one address to be filtered, so
* that our own can be filtered out here. */
if (addr)
lldp->filter_address = *addr;
lldp_rx->filter_address = *addr;
else
zero(lldp->filter_address);
zero(lldp_rx->filter_address);
return 0;
}

View File

@ -21,7 +21,7 @@
#define TEST_LLDP_TYPE_SYSTEM_DESC "systemd-lldp-desc"
static int test_fd[2] = { -1, -1 };
static int lldp_handler_calls;
static int lldp_rx_handler_calls;
int lldp_network_bind_raw_socket(int ifindex) {
if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0)
@ -30,48 +30,48 @@ int lldp_network_bind_raw_socket(int ifindex) {
return test_fd[0];
}
static void lldp_handler(sd_lldp *lldp, sd_lldp_event_t event, sd_lldp_neighbor *n, void *userdata) {
lldp_handler_calls++;
static void lldp_rx_handler(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n, void *userdata) {
lldp_rx_handler_calls++;
}
static int start_lldp(sd_lldp **lldp, sd_event *e, sd_lldp_callback_t cb, void *cb_data) {
static int start_lldp_rx(sd_lldp_rx **lldp_rx, sd_event *e, sd_lldp_rx_callback_t cb, void *cb_data) {
int r;
r = sd_lldp_new(lldp);
r = sd_lldp_rx_new(lldp_rx);
if (r < 0)
return r;
r = sd_lldp_set_ifindex(*lldp, 42);
r = sd_lldp_rx_set_ifindex(*lldp_rx, 42);
if (r < 0)
return r;
r = sd_lldp_set_callback(*lldp, cb, cb_data);
r = sd_lldp_rx_set_callback(*lldp_rx, cb, cb_data);
if (r < 0)
return r;
r = sd_lldp_attach_event(*lldp, e, 0);
r = sd_lldp_rx_attach_event(*lldp_rx, e, 0);
if (r < 0)
return r;
r = sd_lldp_start(*lldp);
r = sd_lldp_rx_start(*lldp_rx);
if (r < 0)
return r;
return 0;
}
static int stop_lldp(sd_lldp *lldp) {
static int stop_lldp_rx(sd_lldp_rx *lldp_rx) {
int r;
r = sd_lldp_stop(lldp);
r = sd_lldp_rx_stop(lldp_rx);
if (r < 0)
return r;
r = sd_lldp_detach_event(lldp);
r = sd_lldp_rx_detach_event(lldp_rx);
if (r < 0)
return r;
sd_lldp_unref(lldp);
sd_lldp_rx_unref(lldp_rx);
safe_close(test_fd[1]);
return 0;
@ -96,7 +96,7 @@ static void test_receive_basic_packet(sd_event *e) {
0x00, 0x00 /* End Of LLDPDU */
};
sd_lldp *lldp;
sd_lldp_rx *lldp_rx;
sd_lldp_neighbor **neighbors;
uint8_t type;
const void *data;
@ -104,13 +104,13 @@ static void test_receive_basic_packet(sd_event *e) {
size_t length;
const char *str;
lldp_handler_calls = 0;
assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
lldp_rx_handler_calls = 0;
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0);
assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
sd_event_run(e, 0);
assert_se(lldp_handler_calls == 1);
assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 1);
assert_se(lldp_rx_handler_calls == 1);
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 1);
assert_se(sd_lldp_neighbor_get_chassis_id(neighbors[0], &type, &data, &length) == 0);
assert_se(type == SD_LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS);
@ -137,11 +137,11 @@ static void test_receive_basic_packet(sd_event *e) {
sd_lldp_neighbor_unref(neighbors[0]);
free(neighbors);
assert_se(stop_lldp(lldp) == 0);
assert_se(stop_lldp_rx(lldp_rx) == 0);
}
static void test_receive_incomplete_packet(sd_event *e) {
sd_lldp *lldp;
sd_lldp_rx *lldp_rx;
sd_lldp_neighbor **neighbors;
uint8_t frame[] = {
/* Ethernet header */
@ -156,19 +156,19 @@ static void test_receive_incomplete_packet(sd_event *e) {
0x00, 0x00 /* End Of LLDPDU */
};
lldp_handler_calls = 0;
assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
lldp_rx_handler_calls = 0;
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0);
assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
sd_event_run(e, 0);
assert_se(lldp_handler_calls == 0);
assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 0);
assert_se(lldp_rx_handler_calls == 0);
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 0);
assert_se(stop_lldp(lldp) == 0);
assert_se(stop_lldp_rx(lldp_rx) == 0);
}
static void test_receive_oui_packet(sd_event *e) {
sd_lldp *lldp;
sd_lldp_rx *lldp_rx;
sd_lldp_neighbor **neighbors;
uint8_t frame[] = {
/* Ethernet header */
@ -197,13 +197,13 @@ static void test_receive_oui_packet(sd_event *e) {
0x00, 0x00 /* End of LLDPDU */
};
lldp_handler_calls = 0;
assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
lldp_rx_handler_calls = 0;
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0);
assert_se(write(test_fd[1], frame, sizeof(frame)) == sizeof(frame));
sd_event_run(e, 0);
assert_se(lldp_handler_calls == 1);
assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 1);
assert_se(lldp_rx_handler_calls == 1);
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 1);
assert_se(sd_lldp_neighbor_tlv_rewind(neighbors[0]) >= 0);
assert_se(sd_lldp_neighbor_tlv_is_type(neighbors[0], SD_LLDP_TYPE_CHASSIS_ID) > 0);
@ -230,7 +230,7 @@ static void test_receive_oui_packet(sd_event *e) {
sd_lldp_neighbor_unref(neighbors[0]);
free(neighbors);
assert_se(stop_lldp(lldp) == 0);
assert_se(stop_lldp_rx(lldp_rx) == 0);
}
static void test_multiple_neighbors_sorted(sd_event *e) {
@ -311,7 +311,7 @@ static void test_multiple_neighbors_sorted(sd_event *e) {
"2/19", "1/0",
};
sd_lldp *lldp;
sd_lldp_rx *lldp_rx;
sd_lldp_neighbor **neighbors;
int i;
uint8_t type;
@ -319,8 +319,8 @@ static void test_multiple_neighbors_sorted(sd_event *e) {
size_t length, expected_length;
uint16_t ttl;
lldp_handler_calls = 0;
assert_se(start_lldp(&lldp, e, lldp_handler, NULL) == 0);
lldp_rx_handler_calls = 0;
assert_se(start_lldp_rx(&lldp_rx, e, lldp_rx_handler, NULL) == 0);
assert_se(write(test_fd[1], frame1, sizeof(frame1)) == sizeof(frame1));
sd_event_run(e, 0);
@ -334,9 +334,9 @@ static void test_multiple_neighbors_sorted(sd_event *e) {
sd_event_run(e, 0);
assert_se(write(test_fd[1], frame6, sizeof(frame6)) == sizeof(frame6));
sd_event_run(e, 0);
assert_se(lldp_handler_calls == 6);
assert_se(lldp_rx_handler_calls == 6);
assert_se(sd_lldp_get_neighbors(lldp, &neighbors) == 6);
assert_se(sd_lldp_rx_get_neighbors(lldp_rx, &neighbors) == 6);
for (i = 0; i < 6; i++) {
assert_se(sd_lldp_neighbor_get_chassis_id(neighbors[i], &type, &data, &length) == 0);
@ -359,7 +359,7 @@ static void test_multiple_neighbors_sorted(sd_event *e) {
sd_lldp_neighbor_unref(neighbors[i]);
free(neighbors);
assert_se(stop_lldp(lldp) == 0);
assert_se(stop_lldp_rx(lldp_rx) == 0);
}
int main(int argc, char *argv[]) {

View File

@ -209,7 +209,7 @@ static void link_free_engines(Link *link) {
link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client);
link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
link->lldp = sd_lldp_unref(link->lldp);
link->lldp_rx = sd_lldp_rx_unref(link->lldp_rx);
link_lldp_emit_stop(link);
ndisc_flush(link);
@ -375,7 +375,7 @@ int link_stop_engines(Link *link, bool may_keep_dhcp) {
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop DHCPv4 server: %m");
k = sd_lldp_stop(link->lldp);
k = sd_lldp_rx_stop(link->lldp_rx);
if (k < 0)
r = log_link_warning_errno(link, k, "Could not stop LLDP: %m");
@ -690,8 +690,8 @@ static int link_acquire_dynamic_conf(Link *link) {
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m");
if (link->lldp) {
r = sd_lldp_start(link->lldp);
if (link->lldp_rx) {
r = sd_lldp_rx_start(link->lldp_rx);
if (r < 0)
return log_link_warning_errno(link, r, "Failed to start LLDP client: %m");
}
@ -2102,8 +2102,8 @@ static int link_update_hardware_address(Link *link, sd_netlink_message *message)
return log_link_debug_errno(link, r, "Could not update MAC for NDisc: %m");
}
if (link->lldp) {
r = sd_lldp_set_filter_address(link->lldp, &link->hw_addr.ether);
if (link->lldp_rx) {
r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
if (r < 0)
return log_link_debug_errno(link, r, "Could not update MAC address for LLDP: %m");
}
@ -2266,8 +2266,8 @@ static int link_update_name(Link *link, sd_netlink_message *message) {
return log_link_debug_errno(link, r, "Failed to update interface name in Router Advertisement: %m");
}
if (link->lldp) {
r = sd_lldp_set_ifname(link->lldp, link->ifname);
if (link->lldp_rx) {
r = sd_lldp_rx_set_ifname(link->lldp_rx, link->ifname);
if (r < 0)
return log_link_debug_errno(link, r, "Failed to update interface name in LLDP: %m");
}

View File

@ -171,7 +171,7 @@ typedef struct Link {
bool dhcp6_pd_prefixes_assigned:1;
/* This is about LLDP reception */
sd_lldp *lldp;
sd_lldp_rx *lldp_rx;
char *lldp_file;
/* This is about LLDP transmission */

View File

@ -48,7 +48,7 @@ static bool link_lldp_rx_enabled(Link *link) {
return link->network->lldp_mode != LLDP_MODE_NO;
}
static void lldp_handler(sd_lldp *lldp, sd_lldp_event_t event, sd_lldp_neighbor *n, void *userdata) {
static void lldp_rx_handler(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n, void *userdata) {
Link *link = userdata;
int r;
@ -56,7 +56,7 @@ static void lldp_handler(sd_lldp *lldp, sd_lldp_event_t event, sd_lldp_neighbor
(void) link_lldp_save(link);
if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) {
if (link_lldp_emit_enabled(link) && event == SD_LLDP_RX_EVENT_ADDED) {
/* If we received information about a new neighbor, restart the LLDP "fast" logic */
log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission.");
@ -73,33 +73,33 @@ int link_lldp_rx_configure(Link *link) {
if (!link_lldp_rx_enabled(link))
return 0;
if (link->lldp)
if (link->lldp_rx)
return -EBUSY;
r = sd_lldp_new(&link->lldp);
r = sd_lldp_rx_new(&link->lldp_rx);
if (r < 0)
return r;
r = sd_lldp_attach_event(link->lldp, link->manager->event, 0);
r = sd_lldp_rx_attach_event(link->lldp_rx, link->manager->event, 0);
if (r < 0)
return r;
r = sd_lldp_set_ifindex(link->lldp, link->ifindex);
r = sd_lldp_rx_set_ifindex(link->lldp_rx, link->ifindex);
if (r < 0)
return r;
r = sd_lldp_match_capabilities(link->lldp,
link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ?
SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS :
SD_LLDP_SYSTEM_CAPABILITIES_ALL);
r = sd_lldp_rx_match_capabilities(link->lldp_rx,
link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ?
SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS :
SD_LLDP_SYSTEM_CAPABILITIES_ALL);
if (r < 0)
return r;
r = sd_lldp_set_filter_address(link->lldp, &link->hw_addr.ether);
r = sd_lldp_rx_set_filter_address(link->lldp_rx, &link->hw_addr.ether);
if (r < 0)
return r;
r = sd_lldp_set_callback(link->lldp, lldp_handler, link);
r = sd_lldp_rx_set_callback(link->lldp_rx, lldp_rx_handler, link);
if (r < 0)
return r;
@ -115,12 +115,12 @@ int link_lldp_save(Link *link) {
assert(link);
assert(link->lldp_file);
if (!link->lldp) {
if (!link->lldp_rx) {
(void) unlink(link->lldp_file);
return 0;
}
r = sd_lldp_get_neighbors(link->lldp, &l);
r = sd_lldp_rx_get_neighbors(link->lldp_rx, &l);
if (r < 0)
return r;
if (r == 0) {

View File

@ -35,7 +35,7 @@ int main(int argc, char **argv) {
test_table(lldp_mode, LLDP_MODE);
test_table(netdev_kind, NETDEV_KIND);
test_table(radv_prefix_delegation, RADV_PREFIX_DELEGATION);
test_table(lldp_event, SD_LLDP_EVENT);
test_table(lldp_rx_event, SD_LLDP_RX_EVENT);
test_table(ndisc_event, SD_NDISC_EVENT);
test_table(dhcp_lease_server_type, SD_DHCP_LEASE_SERVER_TYPE);
@ -43,7 +43,7 @@ int main(int argc, char **argv) {
test_table_sparse(macvlan_mode, NETDEV_MACVLAN_MODE);
test_table_sparse(address_family, ADDRESS_FAMILY);
assert_cc(sizeof(sd_lldp_event_t) == sizeof(int64_t));
assert_cc(sizeof(sd_lldp_rx_event_t) == sizeof(int64_t));
assert_cc(sizeof(sd_ndisc_event_t) == sizeof(int64_t));
assert_cc(sizeof(sd_dhcp_lease_server_type_t) == sizeof(int64_t));

View File

@ -119,43 +119,43 @@ enum {
SD_LLDP_OUI_802_3_SUBTYPE_MAXIMUM_FRAME_SIZE = 4,
};
typedef struct sd_lldp sd_lldp;
typedef struct sd_lldp_rx sd_lldp_rx;
typedef struct sd_lldp_neighbor sd_lldp_neighbor;
typedef enum sd_lldp_event_t {
SD_LLDP_EVENT_ADDED,
SD_LLDP_EVENT_REMOVED,
SD_LLDP_EVENT_UPDATED,
SD_LLDP_EVENT_REFRESHED,
_SD_LLDP_EVENT_MAX,
_SD_LLDP_EVENT_INVALID = -EINVAL,
_SD_ENUM_FORCE_S64(LLDP_EVENT),
} sd_lldp_event_t;
typedef enum sd_lldp_rx_event_t {
SD_LLDP_RX_EVENT_ADDED,
SD_LLDP_RX_EVENT_REMOVED,
SD_LLDP_RX_EVENT_UPDATED,
SD_LLDP_RX_EVENT_REFRESHED,
_SD_LLDP_RX_EVENT_MAX,
_SD_LLDP_RX_EVENT_INVALID = -EINVAL,
_SD_ENUM_FORCE_S64(LLDP_RX_EVENT),
} sd_lldp_rx_event_t;
typedef void (*sd_lldp_callback_t)(sd_lldp *lldp, sd_lldp_event_t event, sd_lldp_neighbor *n, void *userdata);
typedef void (*sd_lldp_rx_callback_t)(sd_lldp_rx *lldp_rx, sd_lldp_rx_event_t event, sd_lldp_neighbor *n, void *userdata);
int sd_lldp_new(sd_lldp **ret);
sd_lldp* sd_lldp_ref(sd_lldp *lldp);
sd_lldp* sd_lldp_unref(sd_lldp *lldp);
int sd_lldp_rx_new(sd_lldp_rx **ret);
sd_lldp_rx *sd_lldp_rx_ref(sd_lldp_rx *lldp_rx);
sd_lldp_rx *sd_lldp_rx_unref(sd_lldp_rx *lldp_rx);
int sd_lldp_start(sd_lldp *lldp);
int sd_lldp_stop(sd_lldp *lldp);
int sd_lldp_rx_start(sd_lldp_rx *lldp_rx);
int sd_lldp_rx_stop(sd_lldp_rx *lldp_rx);
int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int64_t priority);
int sd_lldp_detach_event(sd_lldp *lldp);
sd_event *sd_lldp_get_event(sd_lldp *lldp);
int sd_lldp_rx_attach_event(sd_lldp_rx *lldp_rx, sd_event *event, int64_t priority);
int sd_lldp_rx_detach_event(sd_lldp_rx *lldp_rx);
sd_event *sd_lldp_rx_get_event(sd_lldp_rx *lldp_rx);
int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_callback_t cb, void *userdata);
int sd_lldp_set_ifindex(sd_lldp *lldp, int ifindex);
int sd_lldp_set_ifname(sd_lldp *lldp, const char *ifname);
const char *sd_lldp_get_ifname(sd_lldp *lldp);
int sd_lldp_rx_set_callback(sd_lldp_rx *lldp_rx, sd_lldp_rx_callback_t cb, void *userdata);
int sd_lldp_rx_set_ifindex(sd_lldp_rx *lldp_rx, int ifindex);
int sd_lldp_rx_set_ifname(sd_lldp_rx *lldp_rx, const char *ifname);
const char *sd_lldp_rx_get_ifname(sd_lldp_rx *lldp_rx);
/* Controls how much and what to store in the neighbors database */
int sd_lldp_set_neighbors_max(sd_lldp *lldp, uint64_t n);
int sd_lldp_match_capabilities(sd_lldp *lldp, uint16_t mask);
int sd_lldp_set_filter_address(sd_lldp *lldp, const struct ether_addr *address);
int sd_lldp_rx_set_neighbors_max(sd_lldp_rx *lldp_rx, uint64_t n);
int sd_lldp_rx_match_capabilities(sd_lldp_rx *lldp_rx, uint16_t mask);
int sd_lldp_rx_set_filter_address(sd_lldp_rx *lldp_rx, const struct ether_addr *address);
int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***neighbors);
int sd_lldp_rx_get_neighbors(sd_lldp_rx *lldp_rx, sd_lldp_neighbor ***neighbors);
int sd_lldp_neighbor_from_raw(sd_lldp_neighbor **ret, const void *raw, size_t raw_size);
sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n);
@ -190,7 +190,7 @@ int sd_lldp_neighbor_tlv_get_oui(sd_lldp_neighbor *n, uint8_t oui[_SD_ARRAY_STAT
int sd_lldp_neighbor_tlv_is_oui(sd_lldp_neighbor *n, const uint8_t oui[_SD_ARRAY_STATIC 3], uint8_t subtype);
int sd_lldp_neighbor_tlv_get_raw(sd_lldp_neighbor *n, const void **ret, size_t *size);
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp, sd_lldp_unref);
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_rx, sd_lldp_rx_unref);
_SD_DEFINE_POINTER_CLEANUP_FUNC(sd_lldp_neighbor, sd_lldp_neighbor_unref);
_SD_END_DECLARATIONS;