2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019-2021 IKS Service GmbH
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Lutz Donnerhacke <lutz@donnerhacke.de>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/ctype.h>
38 #include <sys/errno.h>
39 #include <sys/syslog.h>
40 #include <sys/types.h>
41 #include <sys/counter.h>
43 #include <net/ethernet.h>
45 #include <netgraph/ng_message.h>
46 #include <netgraph/ng_parse.h>
47 #include <netgraph/ng_vlan_rotate.h>
48 #include <netgraph/netgraph.h>
51 * This section contains the netgraph method declarations for the
52 * sample node. These methods define the netgraph 'type'.
55 static ng_constructor_t ng_vlanrotate_constructor;
56 static ng_rcvmsg_t ng_vlanrotate_rcvmsg;
57 static ng_shutdown_t ng_vlanrotate_shutdown;
58 static ng_newhook_t ng_vlanrotate_newhook;
59 static ng_rcvdata_t ng_vlanrotate_rcvdata;
60 static ng_disconnect_t ng_vlanrotate_disconnect;
62 /* Parse type for struct ng_vlanrotate_conf */
63 static const struct ng_parse_struct_field ng_vlanrotate_conf_fields[] = {
64 {"rot", &ng_parse_int8_type},
65 {"min", &ng_parse_uint8_type},
66 {"max", &ng_parse_uint8_type},
69 static const struct ng_parse_type ng_vlanrotate_conf_type = {
70 &ng_parse_struct_type,
71 &ng_vlanrotate_conf_fields
74 /* Parse type for struct ng_vlanrotate_stat */
75 static struct ng_parse_fixedarray_info ng_vlanrotate_stat_hist_info = {
76 &ng_parse_uint64_type,
77 NG_VLANROTATE_MAX_VLANS
79 static struct ng_parse_type ng_vlanrotate_stat_hist = {
80 &ng_parse_fixedarray_type,
81 &ng_vlanrotate_stat_hist_info
83 static const struct ng_parse_struct_field ng_vlanrotate_stat_fields[] = {
84 {"drops", &ng_parse_uint64_type},
85 {"excessive", &ng_parse_uint64_type},
86 {"incomplete", &ng_parse_uint64_type},
87 {"histogram", &ng_vlanrotate_stat_hist},
90 static struct ng_parse_type ng_vlanrotate_stat_type = {
91 &ng_parse_struct_type,
92 &ng_vlanrotate_stat_fields
96 /* List of commands and how to convert arguments to/from ASCII */
97 static const struct ng_cmdlist ng_vlanrotate_cmdlist[] = {
99 NGM_VLANROTATE_COOKIE,
100 NGM_VLANROTATE_GET_CONF,
103 &ng_vlanrotate_conf_type,
106 NGM_VLANROTATE_COOKIE,
107 NGM_VLANROTATE_SET_CONF,
109 &ng_vlanrotate_conf_type,
113 NGM_VLANROTATE_COOKIE,
114 NGM_VLANROTATE_GET_STAT,
117 &ng_vlanrotate_stat_type
120 NGM_VLANROTATE_COOKIE,
121 NGM_VLANROTATE_CLR_STAT,
124 &ng_vlanrotate_stat_type
127 NGM_VLANROTATE_COOKIE,
128 NGM_VLANROTATE_GETCLR_STAT,
131 &ng_vlanrotate_stat_type
136 /* Netgraph node type descriptor */
137 static struct ng_type typestruct = {
138 .version = NG_ABI_VERSION,
139 .name = NG_VLANROTATE_NODE_TYPE,
140 .constructor = ng_vlanrotate_constructor,
141 .rcvmsg = ng_vlanrotate_rcvmsg,
142 .shutdown = ng_vlanrotate_shutdown,
143 .newhook = ng_vlanrotate_newhook,
144 .rcvdata = ng_vlanrotate_rcvdata,
145 .disconnect = ng_vlanrotate_disconnect,
146 .cmdlist = ng_vlanrotate_cmdlist,
148 NETGRAPH_INIT(vlanrotate, &typestruct);
150 struct ng_vlanrotate_kernel_stats {
151 counter_u64_t drops, excessive, incomplete;
152 counter_u64_t histogram[NG_VLANROTATE_MAX_VLANS];
155 /* Information we store for each node */
157 hook_p original_hook;
159 hook_p excessive_hook;
160 hook_p incomplete_hook;
161 struct ng_vlanrotate_conf conf;
162 struct ng_vlanrotate_kernel_stats stats;
164 typedef struct vlanrotate *vlanrotate_p;
167 * Set up the private data structure.
170 ng_vlanrotate_constructor(node_p node)
174 vlanrotate_p vrp = malloc(sizeof(*vrp), M_NETGRAPH, M_WAITOK | M_ZERO);
176 vrp->conf.max = NG_VLANROTATE_MAX_VLANS;
178 vrp->stats.drops = counter_u64_alloc(M_WAITOK);
179 vrp->stats.excessive = counter_u64_alloc(M_WAITOK);
180 vrp->stats.incomplete = counter_u64_alloc(M_WAITOK);
181 for (i = 0; i < NG_VLANROTATE_MAX_VLANS; i++)
182 vrp->stats.histogram[i] = counter_u64_alloc(M_WAITOK);
184 NG_NODE_SET_PRIVATE(node, vrp);
189 * Give our ok for a hook to be added.
192 ng_vlanrotate_newhook(node_p node, hook_p hook, const char *name)
194 const vlanrotate_p vrp = NG_NODE_PRIVATE(node);
197 if (strcmp(name, NG_VLANROTATE_HOOK_ORDERED) == 0) {
198 dst = &vrp->ordered_hook;
199 } else if (strcmp(name, NG_VLANROTATE_HOOK_ORIGINAL) == 0) {
200 dst = &vrp->original_hook;
201 } else if (strcmp(name, NG_VLANROTATE_HOOK_EXCESSIVE) == 0) {
202 dst = &vrp->excessive_hook;
203 } else if (strcmp(name, NG_VLANROTATE_HOOK_INCOMPLETE) == 0) {
204 dst = &vrp->incomplete_hook;
206 return (EINVAL); /* not a hook we know about */
209 return (EADDRINUSE); /* don't override */
216 * Get a netgraph control message.
217 * A response is not required.
220 ng_vlanrotate_rcvmsg(node_p node, item_p item, hook_p lasthook)
222 const vlanrotate_p vrp = NG_NODE_PRIVATE(node);
223 struct ng_mesg *resp = NULL;
225 struct ng_vlanrotate_conf *pcf;
228 NGI_GET_MSG(item, msg);
229 /* Deal with message according to cookie and command */
230 switch (msg->header.typecookie) {
231 case NGM_VLANROTATE_COOKIE:
232 switch (msg->header.cmd) {
233 case NGM_VLANROTATE_GET_CONF:
234 NG_MKRESPONSE(resp, msg, sizeof(vrp->conf), M_NOWAIT);
239 *((struct ng_vlanrotate_conf *)resp->data) = vrp->conf;
241 case NGM_VLANROTATE_SET_CONF:
242 if (msg->header.arglen != sizeof(*pcf)) {
247 pcf = (struct ng_vlanrotate_conf *)msg->data;
249 if (pcf->max == 0) /* keep current value */
250 pcf->max = vrp->conf.max;
252 if ((pcf->max > NG_VLANROTATE_MAX_VLANS) ||
253 (pcf->min > pcf->max) ||
254 (abs(pcf->rot) >= pcf->max)) {
261 case NGM_VLANROTATE_GET_STAT:
262 case NGM_VLANROTATE_GETCLR_STAT:
264 struct ng_vlanrotate_stat *p;
267 NG_MKRESPONSE(resp, msg, sizeof(*p), M_NOWAIT);
272 p = (struct ng_vlanrotate_stat *)resp->data;
273 p->drops = counter_u64_fetch(vrp->stats.drops);
274 p->excessive = counter_u64_fetch(vrp->stats.excessive);
275 p->incomplete = counter_u64_fetch(vrp->stats.incomplete);
276 for (i = 0; i < NG_VLANROTATE_MAX_VLANS; i++)
277 p->histogram[i] = counter_u64_fetch(vrp->stats.histogram[i]);
278 if (msg->header.cmd != NGM_VLANROTATE_GETCLR_STAT)
281 case NGM_VLANROTATE_CLR_STAT:
285 counter_u64_zero(vrp->stats.drops);
286 counter_u64_zero(vrp->stats.excessive);
287 counter_u64_zero(vrp->stats.incomplete);
288 for (i = 0; i < NG_VLANROTATE_MAX_VLANS; i++)
289 counter_u64_zero(vrp->stats.histogram[i]);
293 error = EINVAL; /* unknown command */
298 error = EINVAL; /* unknown cookie type */
302 /* Take care of synchronous response, if any */
303 NG_RESPOND_MSG(error, node, item, resp);
304 /* Free the message and return */
310 * Receive data, and do rotate the vlans as desired.
312 * Rotating is quite complicated if the rotation offset and the number
313 * of vlans are not relativly prime. In this case multiple slices need
314 * to be rotated separately.
316 * Rotation can be additive or subtractive. Some examples:
317 * 01234 5 vlans given
323 * First some helper functions ...
326 struct ether_vlan_stack_entry {
331 struct ether_vlan_stack_header {
332 uint8_t dst[ETHER_ADDR_LEN];
333 uint8_t src[ETHER_ADDR_LEN];
334 struct ether_vlan_stack_entry vlan_stack[1];
338 ng_vlanrotate_gcd(int a, int b)
343 return ng_vlanrotate_gcd(b, a % b);
347 ng_vlanrotate_rotate(struct ether_vlan_stack_entry arr[], int d, int n)
350 struct ether_vlan_stack_entry temp;
352 /* for each commensurable slice */
353 for (i = ng_vlanrotate_gcd(d, n); i-- > 0;) {
354 /* rotate left aka downwards */
373 ng_vlanrotate_rcvdata(hook_p hook, item_p item)
375 const vlanrotate_p vrp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
376 struct ether_vlan_stack_header *evsh;
377 struct mbuf *m = NULL;
385 if (hook == vrp->ordered_hook) {
386 rotate = +vrp->conf.rot;
387 dst_hook = vrp->original_hook;
388 } else if (hook == vrp->original_hook) {
389 rotate = -vrp->conf.rot;
390 dst_hook = vrp->ordered_hook;
392 dst_hook = vrp->original_hook;
393 goto send; /* everything else goes out unmodified */
396 if (dst_hook == NULL) {
401 /* count the vlans */
402 for (vlans = 0; vlans <= NG_VLANROTATE_MAX_VLANS; vlans++) {
403 size_t expected_len = sizeof(struct ether_vlan_stack_header)
404 + vlans * sizeof(struct ether_vlan_stack_entry);
406 if (m->m_len < expected_len) {
407 m = m_pullup(m, expected_len);
414 evsh = mtod(m, struct ether_vlan_stack_header *);
415 switch (ntohs(evsh->vlan_stack[vlans].proto)) {
418 case ETHERTYPE_8021Q9100:
419 case ETHERTYPE_8021Q9200:
420 case ETHERTYPE_8021Q9300:
427 if ((vlans > vrp->conf.max) || (vlans >= NG_VLANROTATE_MAX_VLANS)) {
428 counter_u64_add(vrp->stats.excessive, 1);
429 dst_hook = vrp->excessive_hook;
433 if ((vlans < vrp->conf.min) || (vlans <= abs(rotate))) {
434 counter_u64_add(vrp->stats.incomplete, 1);
435 dst_hook = vrp->incomplete_hook;
438 counter_u64_add(vrp->stats.histogram[vlans], 1);
440 /* rotating upwards always (using modular arithmetics) */
443 } else if (rotate > 0) {
444 ng_vlanrotate_rotate(evsh->vlan_stack, rotate, vlans);
446 ng_vlanrotate_rotate(evsh->vlan_stack, vlans + rotate, vlans);
450 if (dst_hook == NULL)
452 NG_FWD_NEW_DATA(error, item, dst_hook, m);
456 counter_u64_add(vrp->stats.drops, 1);
464 * Do local shutdown processing..
465 * All our links and the name have already been removed.
468 ng_vlanrotate_shutdown(node_p node)
470 const vlanrotate_p vrp = NG_NODE_PRIVATE(node);
473 NG_NODE_SET_PRIVATE(node, NULL);
475 counter_u64_free(vrp->stats.drops);
476 counter_u64_free(vrp->stats.excessive);
477 counter_u64_free(vrp->stats.incomplete);
478 for (i = 0; i < NG_VLANROTATE_MAX_VLANS; i++)
479 counter_u64_free(vrp->stats.histogram[i]);
481 free(vrp, M_NETGRAPH);
489 * For this type, removal of the last link destroys the node
492 ng_vlanrotate_disconnect(hook_p hook)
494 const vlanrotate_p vrp = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
496 if (vrp->original_hook == hook)
497 vrp->original_hook = NULL;
498 if (vrp->ordered_hook == hook)
499 vrp->ordered_hook = NULL;
500 if (vrp->excessive_hook == hook)
501 vrp->excessive_hook = NULL;
502 if (vrp->incomplete_hook == hook)
503 vrp->incomplete_hook = NULL;
505 /* during shutdown the node is invalid, don't shutdown twice */
506 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) &&
507 (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
508 ng_rmnode_self(NG_HOOK_NODE(hook));