bond: Add bonding driver and LACP protocol
[vpp.git] / src / plugins / lacp / node.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #define _GNU_SOURCE
17 #include <vnet/bonding/node.h>
18 #include <vnet/ethernet/packet.h>
19 #include <lacp/node.h>
20
21 lacp_state_struct lacp_state_array[] = {
22 #define _(b, s, n) {.bit = b, .str = #s, },
23   foreach_lacp_state_flag
24 #undef _
25   {.str = NULL}
26 };
27
28 static vlib_node_registration_t lacp_process_node;
29
30 /** \file
31
32     2 x LACP graph nodes: an "interior" node to process
33     incoming announcements, and a "process" node to periodically
34     send announcements.
35
36     The interior node is neither pipelined nor dual-looped, because
37     it would be very unusual to see more than one LACP packet in
38     a given input frame. So, it's a very simple / straighforward
39     example.
40 */
41
42 /*
43  * packet counter strings
44  * Dump these counters via the "show error" CLI command
45  */
46 static char *lacp_error_strings[] = {
47 #define _(sym,string) string,
48   foreach_lacp_error
49 #undef _
50 };
51
52 /*
53  * We actually send all lacp pkts to the "error" node after scanning
54  * them, so the graph node has only one next-index. The "error-drop"
55  * node automatically bumps our per-node packet counters for us.
56  */
57 typedef enum
58 {
59   LACP_INPUT_NEXT_NORMAL,
60   LACP_INPUT_N_NEXT,
61 } lacp_next_t;
62
63 /*
64  * Process a frame of lacp packets
65  * Expect 1 packet / frame
66  */
67 static uword
68 lacp_node_fn (vlib_main_t * vm,
69               vlib_node_runtime_t * node, vlib_frame_t * frame)
70 {
71   u32 n_left_from, *from;
72   lacp_input_trace_t *t0;
73   uword n_trace = vlib_get_trace_count (vm, node);
74
75   from = vlib_frame_vector_args (frame);        /* array of buffer indices */
76   n_left_from = frame->n_vectors;       /* number of buffer indices */
77
78   while (n_left_from > 0)
79     {
80       u32 bi0;
81       vlib_buffer_t *b0;
82       u32 next0, error0;
83
84       bi0 = from[0];
85       b0 = vlib_get_buffer (vm, bi0);
86
87       next0 = LACP_INPUT_NEXT_NORMAL;
88
89       /* scan this lacp pkt. error0 is the counter index to bump */
90       error0 = lacp_input (vm, b0, bi0);
91       b0->error = node->errors[error0];
92
93       /* If this pkt is traced, snapshoot the data */
94       if (PREDICT_FALSE (n_trace > 0))
95         {
96           int len;
97           vlib_trace_buffer (vm, node, next0, b0,
98                              /* follow_chain */ 0);
99           vlib_set_trace_count (vm, node, --n_trace);
100           t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
101           len = (b0->current_length < sizeof (t0->pkt))
102             ? b0->current_length : sizeof (t0->pkt);
103           t0->len = len;
104           clib_memcpy (&t0->pkt, vlib_buffer_get_current (b0), len);
105         }
106       /* push this pkt to the next graph node, always error-drop */
107       vlib_set_next_frame_buffer (vm, node, next0, bi0);
108
109       from += 1;
110       n_left_from -= 1;
111     }
112
113   return frame->n_vectors;
114 }
115
116 /*
117  * lacp input graph node declaration
118  */
119 /* *INDENT-OFF* */
120 VLIB_REGISTER_NODE (lacp_input_node, static) = {
121   .function = lacp_node_fn,
122   .name = "lacp-input",
123   .vector_size = sizeof (u32),
124   .type = VLIB_NODE_TYPE_INTERNAL,
125
126   .n_errors = LACP_N_ERROR,
127   .error_strings = lacp_error_strings,
128
129   .format_trace = lacp_input_format_trace,
130
131   .n_next_nodes = LACP_INPUT_N_NEXT,
132   .next_nodes = {
133     [LACP_INPUT_NEXT_NORMAL] = "error-drop",
134   },
135 };
136 /* *INDENT-ON* */
137
138 /*
139  * lacp periodic function
140  */
141 static uword
142 lacp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
143 {
144   lacp_main_t *lm = &lacp_main;
145   f64 poll_time_remaining;
146   uword event_type, *event_data = 0;
147   u8 enabled = 0;
148
149   /* So we can send events to the lacp process */
150   lm->lacp_process_node_index = lacp_process_node.index;
151
152   ethernet_register_input_type (vm, ETHERNET_TYPE_SLOW_PROTOCOLS /* LACP */ ,
153                                 lacp_input_node.index);
154
155   poll_time_remaining = 0.2;
156   while (1)
157     {
158       if (enabled)
159         poll_time_remaining =
160           vlib_process_wait_for_event_or_clock (vm, poll_time_remaining);
161       else
162         vlib_process_wait_for_event (vm);
163
164       event_type = vlib_process_get_events (vm, &event_data);
165       switch (event_type)
166         {
167         case ~0:                /* no events => timeout */
168           break;
169         case LACP_PROCESS_EVENT_START:
170           enabled = 1;
171           break;
172         case LACP_PROCESS_EVENT_STOP:
173           enabled = 0;
174           continue;
175         default:
176           clib_warning ("BUG: event type 0x%wx", event_type);
177           break;
178         }
179       if (event_data)
180         _vec_len (event_data) = 0;
181
182       if (vlib_process_suspend_time_is_zero (poll_time_remaining))
183         {
184           lacp_periodic (vm);
185           poll_time_remaining = 0.2;
186         }
187     }
188
189   return 0;
190 }
191
192 /*
193  * lacp periodic node declaration
194  */
195 /* *INDENT-OFF* */
196 VLIB_REGISTER_NODE (lacp_process_node, static) = {
197   .function = lacp_process,
198   .type = VLIB_NODE_TYPE_PROCESS,
199   .name = "lacp-process",
200 };
201 /* *INDENT-ON* */
202
203 /*
204  * fd.io coding-style-patch-verification: ON
205  *
206  * Local Variables:
207  * eval: (c-set-style "gnu")
208  * End:
209  */