| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <linux/if_arp.h> |
| 4 |
|
| 5 |
#include "sd-netlink.h" |
| 6 |
|
| 7 |
#include "conf-parser.h" |
| 8 |
#include "hsr.h" |
| 9 |
#include "networkd-link.h" |
| 10 |
#include "string-table.h" |
| 11 |
#include "string-util.h" |
| 12 |
#include "strv.h" |
| 13 |
|
| 14 |
static const char * const hsr_protocol_table[_NETDEV_HSR_PROTOCOL_MAX] = { |
| 15 |
[NETDEV_HSR_PROTOCOL_HSR] = "hsr", |
| 16 |
[NETDEV_HSR_PROTOCOL_PRP] = "prp", |
| 17 |
}; |
| 18 |
|
| 19 |
DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(hsr_protocol, HsrProtocol); |
| 20 |
DEFINE_CONFIG_PARSE_ENUM(config_parse_hsr_protocol, hsr_protocol, HsrProtocol); |
| 21 |
|
| 22 |
static int hsr_get_port_links(NetDev *netdev, Link **ret1, Link **ret2) { |
| 23 |
Hsr *h = ASSERT_PTR(HSR(netdev)); |
| 24 |
Link *link1, *link2; |
| 25 |
int r; |
| 26 |
|
| 27 |
r = link_get_by_name(netdev->manager, h->ports[0], &link1); |
| 28 |
if (r < 0) |
| 29 |
return r; |
| 30 |
|
| 31 |
r = link_get_by_name(netdev->manager, h->ports[1], &link2); |
| 32 |
if (r < 0) |
| 33 |
return r; |
| 34 |
|
| 35 |
if (ret1) |
| 36 |
*ret1 = link1; |
| 37 |
if (ret2) |
| 38 |
*ret2 = link2; |
| 39 |
|
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static int netdev_hsr_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) { |
| 44 |
Hsr *h = ASSERT_PTR(HSR(netdev)); |
| 45 |
Link *link1, *link2; |
| 46 |
int r; |
| 47 |
|
| 48 |
assert(m); |
| 49 |
|
| 50 |
r = hsr_get_port_links(netdev, &link1, &link2); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
if (link1->ifindex == link2->ifindex) |
| 55 |
return log_netdev_warning_errno( |
| 56 |
netdev, SYNTHETIC_ERRNO(EINVAL), "the two HSR ports must be different"); |
| 57 |
|
| 58 |
r = sd_netlink_message_append_u32(m, IFLA_HSR_SLAVE1, link1->ifindex); |
| 59 |
if (r < 0) |
| 60 |
return r; |
| 61 |
|
| 62 |
r = sd_netlink_message_append_u32(m, IFLA_HSR_SLAVE2, link2->ifindex); |
| 63 |
if (r < 0) |
| 64 |
return r; |
| 65 |
|
| 66 |
r = sd_netlink_message_append_u8(m, IFLA_HSR_PROTOCOL, h->protocol); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
|
| 70 |
r = sd_netlink_message_append_u8(m, IFLA_HSR_MULTICAST_SPEC, h->supervision); |
| 71 |
if (r < 0) |
| 72 |
return r; |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
static int netdev_hsr_config_verify(NetDev *netdev, const char *filename) { |
| 78 |
Hsr *h = ASSERT_PTR(HSR(netdev)); |
| 79 |
|
| 80 |
assert(filename); |
| 81 |
|
| 82 |
if (strv_length(h->ports) != 2) |
| 83 |
return log_netdev_warning_errno( |
| 84 |
netdev, |
| 85 |
SYNTHETIC_ERRNO(EINVAL), |
| 86 |
"HSR needs two ports set, ignoring \"%s\".", |
| 87 |
filename); |
| 88 |
|
| 89 |
if (streq(h->ports[0], h->ports[1])) |
| 90 |
return log_netdev_warning_errno( |
| 91 |
netdev, |
| 92 |
SYNTHETIC_ERRNO(EINVAL), |
| 93 |
"the two HSR ports must be different, ignoring \"%s\".", |
| 94 |
filename); |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static int netdev_hsr_is_ready_to_create(NetDev *netdev, Link *link) { |
| 100 |
return hsr_get_port_links(netdev, NULL, NULL) >= 0; |
| 101 |
} |
| 102 |
|
| 103 |
static void netdev_hsr_done(NetDev *netdev) { |
| 104 |
Hsr *h = ASSERT_PTR(HSR(netdev)); |
| 105 |
|
| 106 |
strv_free(h->ports); |
| 107 |
} |
| 108 |
|
| 109 |
static void netdev_hsr_init(NetDev *netdev) { |
| 110 |
Hsr *h = ASSERT_PTR(HSR(netdev)); |
| 111 |
|
| 112 |
h->protocol = NETDEV_HSR_PROTOCOL_HSR; |
| 113 |
} |
| 114 |
|
| 115 |
const NetDevVTable hsr_vtable = { |
| 116 |
.object_size = sizeof(Hsr), |
| 117 |
.init = netdev_hsr_init, |
| 118 |
.done = netdev_hsr_done, |
| 119 |
.config_verify = netdev_hsr_config_verify, |
| 120 |
.is_ready_to_create = netdev_hsr_is_ready_to_create, |
| 121 |
.fill_message_create = netdev_hsr_fill_message_create, |
| 122 |
.sections = NETDEV_COMMON_SECTIONS "HSR\0", |
| 123 |
.create_type = NETDEV_CREATE_INDEPENDENT, |
| 124 |
.iftype = ARPHRD_ETHER, |
| 125 |
.generate_mac = true, |
| 126 |
}; |