-
Notifications
You must be signed in to change notification settings - Fork 43
/
lldp_evb.c
461 lines (393 loc) · 12.3 KB
/
lldp_evb.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/******************************************************************************
Implementation of EVB TLVs for LLDP
(c) Copyright IBM Corp. 2010, 2012
Author(s): Jens Osterkamp <jens at linux.vnet.ibm.com>
Author(s): Thomas Richter <tmricht at linux.vnet.ibm.com>
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
******************************************************************************/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include "lldp.h"
#include "lldp_tlv.h"
#include "lldp_evb.h"
#include "lldp_evb_cmds.h"
#include "qbg_vdp.h"
#include "messages.h"
#include "config.h"
struct evb_data *evb_data(char *ifname, enum agent_type type)
{
struct evb_user_data *ud;
struct evb_data *ed = NULL;
ud = find_module_user_data_by_id(&lldp_mod_head, LLDP_MOD_EVB);
if (ud) {
LIST_FOREACH(ed, &ud->head, entry) {
if (!strncmp(ifname, ed->ifname, IFNAMSIZ) &&
(type == ed->agenttype))
return ed;
}
}
return NULL;
}
static void evb_print_tlvinfo(char *ifname, struct tlv_info_evb *tlv)
{
LLDPAD_INFO("%s evb supported/configured forwarding mode: %#02x/%#02x "
"capabilities: %#02x/%#02x vsis: %04i/%04i "
"rte: %02i\n", ifname,
tlv->smode, tlv->cmode, tlv->scap, tlv->ccap,
ntohs(tlv->svsi), ntohs(tlv->cvsi), tlv->rte);
}
static void evb_dump_tlv(char *ifname, struct unpacked_tlv *tlv)
{
int i, left = 0;
char buffer[256];
for (i = 0; i < tlv->length; i++) {
int c;
c = snprintf(buffer + left,
sizeof buffer - left,
"%02x ", tlv->info[i]);
if (c < 0 || (c >= (int)sizeof buffer - left))
break;
else
left += c;
}
LLDPAD_DBG("%s:%s type %i length %i info %s\n",
__func__, ifname, tlv->type, tlv->length, buffer);
}
/*
* Checks values received in TLV and takes over some values.
* Sets the new suggestion in member tie to be send out to switch.
*/
static void evb_update_tlv(struct evb_data *ed)
{
struct tlv_info_evb *recv = &ed->last;
u8 valid = recv->smode &
(LLDP_EVB_CAPABILITY_FORWARD_STANDARD |
LLDP_EVB_CAPABILITY_FORWARD_REFLECTIVE_RELAY);
/*
* Waiting for valid packets to pour in if valid packet was received,
* - check parameters with what we have offered for this interface,
* - fill structure with data,
* - enable local tx
*/
if (!valid) {
LLDPAD_ERR("Neither standard nor rr set as forwarding modes ");
LLDPAD_ERR("for interface - %s\n", ed->ifname);
return;
}
/*
* Check bridge capabilities against local policy
* if bridge supports RR and we support it as well, request it
* by setting smode in tlv to be sent out (ed->tie.smode)
*/
if ((recv->smode & ed->policy.smode) &
LLDP_EVB_CAPABILITY_FORWARD_REFLECTIVE_RELAY)
ed->tie.cmode = LLDP_EVB_CAPABILITY_FORWARD_REFLECTIVE_RELAY;
else
ed->tie.cmode = LLDP_EVB_CAPABILITY_FORWARD_STANDARD;
/* If both sides support RTE, support and configure it */
if ((recv->scap & ed->policy.scap) & LLDP_EVB_CAPABILITY_PROTOCOL_RTE)
ed->tie.ccap |= LLDP_EVB_CAPABILITY_PROTOCOL_RTE;
else
ed->tie.ccap &= ~LLDP_EVB_CAPABILITY_PROTOCOL_RTE;
if ((ed->tie.scap & LLDP_EVB_CAPABILITY_PROTOCOL_RTE) &&
(recv->rte > 0) && (ed->policy.rte > 0))
ed->tie.rte = MAX(ed->policy.rte, recv->rte);
/* If both sides support ECP, set it */
if ((recv->scap & ed->policy.scap) & LLDP_EVB_CAPABILITY_PROTOCOL_ECP)
ed->tie.ccap |= LLDP_EVB_CAPABILITY_PROTOCOL_ECP;
else
ed->tie.ccap &= ~LLDP_EVB_CAPABILITY_PROTOCOL_ECP;
/* If both sides support VDP, set it. Also set number of VSIs */
if ((recv->scap & ed->policy.scap) & LLDP_EVB_CAPABILITY_PROTOCOL_VDP) {
ed->tie.ccap |= LLDP_EVB_CAPABILITY_PROTOCOL_VDP;
ed->tie.cvsi = htons(vdp_vsis(ed->ifname));
ed->tie.svsi = ed->policy.svsi;
} else {
ed->tie.ccap &= ~LLDP_EVB_CAPABILITY_PROTOCOL_VDP;
ed->tie.cvsi = 0;
ed->tie.svsi = 0;
}
}
/*
* Build the packed EVB TLV.
* Returns a pointer to the packed tlv or 0 on failure.
*/
static struct packed_tlv *evb_build_tlv(struct evb_data *ed)
{
struct packed_tlv *ptlv = 0;
u8 infobuf[sizeof(struct tlv_info_evb)];
struct unpacked_tlv tlv = {
.type = ORG_SPECIFIC_TLV,
.length = sizeof(struct tlv_info_evb),
.info = infobuf
};
if (!ed->txmit)
return ptlv;
evb_update_tlv(ed);
memcpy(tlv.info, &ed->tie, tlv.length);
ptlv = pack_tlv(&tlv);
if (ptlv) {
LLDPAD_DBG("%s:%s TLV about to be sent out:\n", __func__,
ed->ifname);
evb_dump_tlv(ed->ifname, &tlv);
} else
LLDPAD_DBG("%s:%s failed to pack EVB TLV\n", __func__,
ed->ifname);
return ptlv;
}
/*
* Function call to build and return module specific packed EVB TLV.
* Returned packed_tlv is free'ed by caller of this function.
*/
static struct packed_tlv *evb_gettlv(struct port *port,
struct lldp_agent *agent)
{
struct evb_data *ed;
if (agent->type != NEAREST_CUSTOMER_BRIDGE)
return 0;
ed = evb_data(port->ifname, agent->type);
if (!ed) {
LLDPAD_ERR("%s:%s agent %d failed\n", __func__, port->ifname,
agent->type);
return 0;
}
return evb_build_tlv(ed);
}
/*
* evb_rchange: process received EVB TLV LLDPDU
*
* TLV not consumed on error
*/
static int evb_rchange(struct port *port, struct lldp_agent *agent,
struct unpacked_tlv *tlv)
{
struct evb_data *ed;
u8 oui_subtype[OUI_SUB_SIZE] = LLDP_OUI_SUBTYPE;
if (agent->type != NEAREST_CUSTOMER_BRIDGE)
return SUBTYPE_INVALID;
ed = evb_data(port->ifname, agent->type);
if (!ed)
return SUBTYPE_INVALID;
if (tlv->type == TYPE_127) {
/* check for length */
if (tlv->length < OUI_SUB_SIZE)
return TLV_ERR;
/* check for oui */
if (memcmp(tlv->info, &oui_subtype, OUI_SUB_SIZE))
return SUBTYPE_INVALID;
/* disable rx if tx has been disabled by administrator */
if (!ed->txmit) {
LLDPAD_WARN("%s:%s agent %d EVB Config disabled\n",
__func__, ed->ifname, agent->type);
return SUBTYPE_INVALID;
}
LLDPAD_DBG("%s:%s agent %d received tlv:\n", __func__,
port->ifname, agent->type);
evb_dump_tlv(ed->ifname, tlv);
memcpy(&ed->last, tlv->info, tlv->length);
evb_print_tlvinfo(ed->ifname, &ed->last);
evb_update_tlv(ed);
somethingChangedLocal(ed->ifname, agent->type);
LLDPAD_DBG("%s:%s agent %d new tlv:\n", __func__,
port->ifname, agent->type);
evb_print_tlvinfo(ed->ifname, &ed->tie);
vdp_update(port->ifname, ed->tie.ccap);
}
return SUBTYPE_INVALID;
}
/*
* Stop all modules which depend on EVB capabilities.
*/
static void evb_stop_modules(char *ifname, struct lldp_agent *agent)
{
LLDPAD_DBG("%s:%s agent %d STOP\n", __func__, ifname, agent->type);
vdp_ifdown(ifname, agent);
}
static void evb_ifdown(char *ifname, struct lldp_agent *agent)
{
struct evb_data *ed;
if (agent->type != NEAREST_CUSTOMER_BRIDGE)
return;
LLDPAD_DBG("%s:%s agent %d called\n", __func__, ifname, agent->type);
ed = evb_data(ifname, agent->type);
if (!ed) {
LLDPAD_DBG("%s:%s agent %d does not exist.\n", __func__,
ifname, agent->type);
return;
}
if (ed->vdp_start)
evb_stop_modules(ifname, agent);
LIST_REMOVE(ed, entry);
free(ed);
LLDPAD_INFO("%s:%s agent %d removed\n", __func__, ifname, agent->type);
}
/*
* Fill up evb structure with reasonable info from the configuration file.
*/
static void evb_init_tlv(struct evb_data *ed, struct lldp_agent *agent)
{
memset(&ed->last, 0, sizeof ed->last);
memset(&ed->tie, 0, sizeof ed->tie);
memset(&ed->policy, 0, sizeof ed->policy);
ed->txmit = evb_conf_enabletx(ed->ifname, agent->type);
if (!ed->txmit)
LLDPAD_DBG("%s:%s agent %d EVB tx is currently disabled\n",
__func__, ed->ifname, agent->type);
hton24(ed->policy.oui, LLDP_MOD_EVB);
/* Get fmode/capabilities/rte/vsi from configuration file into policy */
ed->policy.smode = evb_conf_fmode(ed->ifname, agent->type);
ed->policy.scap = evb_conf_capa(ed->ifname, agent->type);
ed->policy.rte = evb_conf_rte(ed->ifname, agent->type);
ed->policy.svsi = htons(evb_conf_vsis(ed->ifname, agent->type));
ed->policy.cmode = 0;
ed->policy.ccap = 0;
hton24(ed->tie.oui, LLDP_MOD_EVB);
ed->tie.smode = ed->policy.smode;
ed->tie.cmode = 0;
ed->tie.scap = ed->policy.scap;
ed->tie.ccap = 0;
ed->tie.svsi = ed->policy.svsi;
ed->tie.cvsi = 0;
ed->tie.rte = ed->policy.rte;
ed->last.smode = LLDP_EVB_CAPABILITY_FORWARD_STANDARD |
LLDP_EVB_CAPABILITY_FORWARD_REFLECTIVE_RELAY;
evb_update_tlv(ed);
}
static void evb_ifup(char *ifname, struct lldp_agent *agent)
{
struct evb_data *ed;
struct evb_user_data *ud;
if (agent->type != NEAREST_CUSTOMER_BRIDGE)
return;
LLDPAD_DBG("%s:%s agent %d called\n", __func__, ifname, agent->type);
ed = evb_data(ifname, agent->type);
if (ed) {
LLDPAD_DBG("%s:%s agent %d already exists\n", __func__, ifname,
agent->type);
return;
}
/* not found, alloc/init per-port tlv data */
ed = (struct evb_data *) calloc(1, sizeof(struct evb_data));
if (!ed) {
LLDPAD_ERR("%s:%s agent %d malloc %zu failed\n",
__func__, ifname, agent->type, sizeof(*ed));
return;
}
STRNCPY_TERMINATED(ed->ifname, ifname, IFNAMSIZ);
ed->agenttype = agent->type;
evb_init_tlv(ed, agent);
ud = find_module_user_data_by_id(&lldp_mod_head, LLDP_MOD_EVB);
LIST_INSERT_HEAD(&ud->head, ed, entry);
LLDPAD_DBG("%s:%s agent %d added\n", __func__, ifname, agent->type);
}
static u8 evb_mibdelete(struct port *port, struct lldp_agent *agent)
{
struct evb_data *ed;
ed = evb_data(port->ifname, agent->type);
if (ed && (agent->type == ed->agenttype)) {
memset(&ed->last, 0, sizeof ed->last);
vdp_update(port->ifname, 0);
}
return 0;
}
/*
* Start all modules which depend on EVB capabilities: ECP, VDP, CDCP.
*/
static void evb_start_modules(char *ifname, struct lldp_agent *agent)
{
LLDPAD_DBG("%s:%s agent %d START\n", __func__, ifname, agent->type);
vdp_ifup(ifname, agent);
}
/*
* Check for stable interfaces. When an interface goes up the carrier might
* come and go during a start up time. Define a window during which the port
* is considered unstable for EVB/VDP protocols.
*
* Use the dormantDelay counter of the port to determine a stable interface.
*/
int evb_timer(struct port *port, struct lldp_agent *agent)
{
struct evb_data *ed;
int bits = LLDP_EVB_CAPABILITY_PROTOCOL_ECP |
LLDP_EVB_CAPABILITY_PROTOCOL_VDP;
if (agent->type != NEAREST_CUSTOMER_BRIDGE)
return 0;
ed = evb_data(port->ifname, agent->type);
if (!ed)
return 0;
if (!ed->vdp_start &&
(port->dormantDelay == 1 || (bits & ed->tie.ccap) == bits)) {
ed->vdp_start = true;
evb_start_modules(port->ifname, agent);
vdp_update(port->ifname, ed->tie.ccap);
}
return 0;
}
/*
* Remove all interface/agent specific evb data.
*/
static void evb_free_data(struct evb_user_data *ud)
{
struct evb_data *ed;
if (ud) {
while (!LIST_EMPTY(&ud->head)) {
ed = LIST_FIRST(&ud->head);
LIST_REMOVE(ed, entry);
free(ed);
}
}
}
void evb_unregister(struct lldp_module *mod)
{
if (mod->data) {
evb_free_data((struct evb_user_data *) mod->data);
free(mod->data);
}
free(mod);
LLDPAD_DBG("%s:done\n", __func__);
}
static const struct lldp_mod_ops evb_ops = {
.lldp_mod_register = evb_register,
.lldp_mod_unregister = evb_unregister,
.lldp_mod_gettlv = evb_gettlv,
.lldp_mod_rchange = evb_rchange,
.lldp_mod_ifup = evb_ifup,
.lldp_mod_ifdown = evb_ifdown,
.lldp_mod_mibdelete = evb_mibdelete,
.timer = evb_timer,
.get_arg_handler = evb_get_arg_handlers
};
struct lldp_module *evb_register(void)
{
struct lldp_module *mod;
struct evb_user_data *ud;
mod = calloc(1, sizeof *mod);
if (!mod) {
LLDPAD_ERR("%s: failed to malloc module data\n", __func__);
return NULL;
}
ud = calloc(1, sizeof(struct evb_user_data));
if (!ud) {
free(mod);
LLDPAD_ERR("%s failed to malloc module user data\n", __func__);
return NULL;
}
LIST_INIT(&ud->head);
mod->id = LLDP_MOD_EVB;
mod->ops = &evb_ops;
mod->data = ud;
LLDPAD_DBG("%s:done\n", __func__);
return mod;
}