-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_cmds.c
227 lines (182 loc) · 5.12 KB
/
server_cmds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
// SPDX-License-Identifier: (GPL-2.0)
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <linux/types.h>
#include <net/if.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdbool.h>
#include "libnetlink.h"
#include "server_cmds.h"
#include "state_machine.h"
#include "list.h"
#include "netlink.h"
#include "cfm_netlink.h"
#include "print.h"
static struct rtnl_handle rth;
static ev_io netlink_watcher;
int CTL_addmrp(int br_index, int ring_nr, int pport, int sport, int ring_role,
uint16_t prio, uint8_t ring_recv, uint8_t react_on_link_change,
int in_role, uint16_t in_id, int iport, int in_mode,
uint8_t in_recv, int cfm_instance, int cfm_level, int cfm_mepid,
int cfm_peer_mepid, char *cfm_maid, char *cfm_dmac)
{
return mrp_add(br_index, ring_nr, pport, sport, ring_role, prio,
ring_recv, react_on_link_change, in_role, in_id,
iport, in_mode, in_recv, cfm_instance, cfm_level,
cfm_mepid, cfm_peer_mepid, cfm_maid, cfm_dmac);
}
int CTL_delmrp(int br_index, int ring_nr)
{
return mrp_del(br_index, ring_nr);
}
int CTL_getmrp(int *count, struct mrp_status *status)
{
return mrp_get(count, status);
}
static int netlink_listen(struct rtnl_ctrl_data *who, struct nlmsghdr *n,
void *arg)
{
struct rtattr *infotb[IFLA_BRIDGE_CFM_MEP_STATUS_MAX + 1];
struct rtattr *aftb[IFLA_BRIDGE_MAX + 1];
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr * tb[IFLA_MAX + 1];
struct mrp_port *port;
int len = n->nlmsg_len;
int af_family;
int rem, instance;
struct rtattr *i, *list;
if (n->nlmsg_type == NLMSG_DONE)
return 0;
len -= NLMSG_LENGTH(sizeof(*ifi));
if (len < 0)
return -1;
af_family = ifi->ifi_family;
port = mrp_get_port(ifi->ifi_index);
if (af_family != AF_BRIDGE && af_family != AF_UNSPEC)
return 0;
if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
return 0;
parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
if (tb[IFLA_IFNAME] == NULL) {
pr_err("BUG: nil ifname");
return -1;
}
if (tb[IFLA_ADDRESS]) {
mrp_mac_change(ifi->ifi_index,
(__u8*)RTA_DATA(tb[IFLA_ADDRESS]));
}
if (tb[IFLA_AF_SPEC]) {
parse_rtattr_flags(aftb, IFLA_BRIDGE_MAX, RTA_DATA(tb[IFLA_AF_SPEC]),
RTA_PAYLOAD(tb[IFLA_AF_SPEC]), NLA_F_NESTED);
if (!aftb[IFLA_BRIDGE_CFM])
goto mrp_process;
list = aftb[IFLA_BRIDGE_CFM];
rem = RTA_PAYLOAD(list);
instance = 0xFFFFFFFF;
for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
if (i->rta_type != (IFLA_BRIDGE_CFM_CC_PEER_STATUS_INFO | NLA_F_NESTED))
continue;
parse_rtattr_flags(infotb, IFLA_BRIDGE_CFM_CC_PEER_STATUS_MAX, RTA_DATA(i), RTA_PAYLOAD(i), NLA_F_NESTED);
if (!infotb[IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE])
continue;
if (instance != rta_getattr_u32(infotb[IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE])) {
instance = rta_getattr_u32(infotb[IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE]);
}
mrp_cfm_link_change(ifi->ifi_index, rta_getattr_u32(infotb[IFLA_BRIDGE_CFM_CC_PEER_STATUS_PEER_MEPID]),
rta_getattr_u32(infotb[IFLA_BRIDGE_CFM_CC_PEER_STATUS_CCM_DEFECT]));
}
}
mrp_process:
if (!port)
return 0;
if (tb[IFLA_OPERSTATE]) {
__u8 state = *(__u8*)RTA_DATA(tb[IFLA_OPERSTATE]);
if (port->operstate != state) {
port->operstate = state;
switch (state) {
case IF_OPER_UNKNOWN:
case IF_OPER_NOTPRESENT:
case IF_OPER_DOWN:
case IF_OPER_LOWERLAYERDOWN:
case IF_OPER_TESTING:
case IF_OPER_DORMANT:
mrp_port_link_change(port, false);
break;
case IF_OPER_UP:
mrp_port_link_change(port, true);
break;
}
}
}
if (tb[IFLA_PROTINFO]) {
struct rtattr *aftb[IFLA_BRPORT_MAX+1];
parse_rtattr_nested(aftb, IFLA_BRPORT_MAX, tb[IFLA_PROTINFO]);
if (aftb[IFLA_BRPORT_MRP_RING_OPEN]) {
mrp_port_ring_open(port,
rta_getattr_u8(aftb[IFLA_BRPORT_MRP_RING_OPEN]));
}
if (aftb[IFLA_BRPORT_MRP_IN_OPEN]) {
mrp_port_in_open(port,
rta_getattr_u8(aftb[IFLA_BRPORT_MRP_IN_OPEN]));
}
}
if (!tb[IFLA_MASTER]) {
mrp_destroy(port->mrp->ifindex, port->mrp->ring_nr, false);
}
return 0;
}
static void netlink_rcv(EV_P_ ev_io *w, int revents)
{
rtnl_listen(&rth, netlink_listen, stdout);
}
static int netlink_init(void)
{
int err;
err = rtnl_open(&rth, RTMGRP_LINK);
if (err)
return err;
fcntl(rth.fd, F_SETFL, O_NONBLOCK);
ev_io_init(&netlink_watcher, netlink_rcv, rth.fd, EV_READ);
ev_io_start(EV_DEFAULT, &netlink_watcher);
return 0;
}
static void netlink_uninit(void)
{
ev_io_stop(EV_DEFAULT, &netlink_watcher);
rtnl_close(&rth);
}
int CTL_init(void)
{
if (netlink_init()) {
pr_err("netlink init failed!");
return -1;
}
if (if_init()) {
pr_err("if init failed");
return -1;
}
if (mrp_netlink_init()) {
pr_err("mrp netlink init failed");
return -1;
}
if (cfm_offload_init()) {
pr_err("cfm offload init failed");
return -1;
}
return 0;
}
void CTL_cleanup(void)
{
mrp_netlink_uninit();
netlink_uninit();
mrp_uninit();
}