bond: Add bonding driver and LACP protocol
[vpp.git] / src / plugins / lacp / input.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 <lacp/node.h>
19
20 static int
21 lacp_packet_scan (vlib_main_t * vm, slave_if_t * sif)
22 {
23   lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
24
25   if (lacpdu->subtype != LACP_SUBTYPE)
26     return LACP_ERROR_UNSUPPORTED;
27
28   /*
29    * According to the spec, no checking on the version number and tlv types.
30    * But we may check the tlv lengths.
31    */
32   if ((lacpdu->actor.tlv_length != sizeof (lacp_actor_partner_t)) ||
33       (lacpdu->partner.tlv_length != sizeof (lacp_actor_partner_t)) ||
34       (lacpdu->collector.tlv_length != sizeof (lacp_collector_t)) ||
35       (lacpdu->terminator.tlv_length != 0))
36     return (LACP_ERROR_BAD_TLV);
37
38   lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
39                          LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
40
41   return LACP_ERROR_NONE;
42 }
43
44 static void
45 marker_fill_pdu (marker_pdu_t * marker, slave_if_t * sif)
46 {
47   marker_pdu_t *pkt = (marker_pdu_t *) sif->last_marker_pkt;
48
49   marker->marker_info = pkt->marker_info;
50   marker->marker_info.tlv_type = MARKER_RESPONSE_INFORMATION;
51 }
52
53 void
54 marker_fill_request_pdu (marker_pdu_t * marker, slave_if_t * sif)
55 {
56   marker->marker_info.tlv_type = MARKER_INFORMATION;
57   marker->marker_info.requester_port = sif->actor.port_number;
58   clib_memcpy (marker->marker_info.requester_system, sif->actor.system, 6);
59   marker->marker_info.requester_transaction_id = sif->marker_tx_id;
60   sif->marker_tx_id++;
61 }
62
63 static void
64 send_ethernet_marker_response_pdu (slave_if_t * sif)
65 {
66   lacp_main_t *lm = &lacp_main;
67   u32 *to_next;
68   ethernet_marker_pdu_t *h0;
69   vnet_hw_interface_t *hw;
70   u32 bi0;
71   vlib_buffer_t *b0;
72   vlib_frame_t *f;
73   vlib_main_t *vm = lm->vlib_main;
74   vnet_main_t *vnm = lm->vnet_main;
75
76   /*
77    * see lacp_periodic_init() to understand what's already painted
78    * into the buffer by the packet template mechanism
79    */
80   h0 = vlib_packet_template_get_packet
81     (vm, &lm->marker_packet_templates[sif->packet_template_index], &bi0);
82
83   if (!h0)
84     return;
85
86   /* Add the interface's ethernet source address */
87   hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
88
89   clib_memcpy (h0->ethernet.src_address, hw->hw_address,
90                vec_len (hw->hw_address));
91
92   marker_fill_pdu (&h0->marker, sif);
93
94   /* Set the outbound packet length */
95   b0 = vlib_get_buffer (vm, bi0);
96   b0->current_length = sizeof (ethernet_marker_pdu_t);
97   b0->current_data = 0;
98   b0->total_length_not_including_first_buffer = 0;
99
100   /* And the outbound interface */
101   vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
102
103   /* And output the packet on the correct interface */
104   f = vlib_get_frame_to_node (vm, hw->output_node_index);
105
106   to_next = vlib_frame_vector_args (f);
107   to_next[0] = bi0;
108   f->n_vectors = 1;
109
110   vlib_put_frame_to_node (vm, hw->output_node_index, f);
111 }
112
113 static int
114 handle_marker_protocol (vlib_main_t * vm, slave_if_t * sif)
115 {
116   marker_pdu_t *marker = (marker_pdu_t *) sif->last_marker_pkt;
117
118   /*
119    * According to the spec, no checking on the version number and tlv types.
120    * But we may check the tlv lengths.
121    */
122   if ((marker->marker_info.tlv_length != sizeof (marker_information_t)) ||
123       (marker->terminator.tlv_length != 0))
124     return (LACP_ERROR_BAD_TLV);
125
126   send_ethernet_marker_response_pdu (sif);
127
128   return LACP_ERROR_NONE;
129 }
130
131 /*
132  * lacp input routine
133  */
134 lacp_error_t
135 lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
136 {
137   lacp_main_t *lm = &lacp_main;
138   slave_if_t *sif;
139   uword nbytes;
140   lacp_error_t e;
141   marker_pdu_t *marker;
142   uword last_packet_signature;
143   bond_if_t *bif;
144
145   sif =
146     bond_get_slave_by_sw_if_index (vnet_buffer (b0)->sw_if_index[VLIB_RX]);
147   if ((sif == 0) || (sif->mode != BOND_MODE_LACP))
148     {
149       return LACP_ERROR_DISABLED;
150     }
151
152   /* Handle marker protocol */
153   marker = (marker_pdu_t *) (b0->data + b0->current_data);
154   if (marker->subtype == MARKER_SUBTYPE)
155     {
156       if (sif->last_marker_pkt)
157         _vec_len (sif->last_marker_pkt) = 0;
158       vec_validate (sif->last_marker_pkt,
159                     vlib_buffer_length_in_chain (vm, b0) - 1);
160       nbytes = vlib_buffer_contents (vm, bi0, sif->last_marker_pkt);
161       ASSERT (nbytes <= vec_len (sif->last_marker_pkt));
162       if (nbytes < sizeof (lacp_pdu_t))
163         return LACP_ERROR_TOO_SMALL;
164       return (handle_marker_protocol (vm, sif));
165     }
166
167   /*
168    * typical clib idiom. Don't repeatedly allocate and free
169    * the per-neighbor rx buffer. Reset its apparent length to zero
170    * and reuse it.
171    */
172   if (sif->last_rx_pkt)
173     _vec_len (sif->last_rx_pkt) = 0;
174
175   /*
176    * Make sure the per-neighbor rx buffer is big enough to hold
177    * the data we're about to copy
178    */
179   vec_validate (sif->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1);
180
181   /*
182    * Coalesce / copy the buffer chain into the per-neighbor
183    * rx buffer
184    */
185   nbytes = vlib_buffer_contents (vm, bi0, sif->last_rx_pkt);
186   ASSERT (nbytes <= vec_len (sif->last_rx_pkt));
187
188   if (nbytes < sizeof (lacp_pdu_t))
189     {
190       return LACP_ERROR_TOO_SMALL;
191     }
192
193   last_packet_signature =
194     hash_memory (sif->last_rx_pkt, vec_len (sif->last_rx_pkt), 0xd00b);
195
196   bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
197   if (sif->last_packet_signature_valid &&
198       (sif->last_packet_signature == last_packet_signature) &&
199       hash_get (bif->active_slave_by_sw_if_index, sif->sw_if_index))
200     {
201       lacp_start_current_while_timer (lm->vlib_main, sif,
202                                       sif->ttl_in_seconds);
203       e = LACP_ERROR_CACHE_HIT;
204     }
205   else
206     {
207       /* Actually scan the packet */
208       e = lacp_packet_scan (vm, sif);
209       sif->last_packet_signature_valid = 1;
210       sif->last_packet_signature = last_packet_signature;
211     }
212
213   if (sif->last_rx_pkt)
214     _vec_len (sif->last_rx_pkt) = 0;
215
216   return e;
217 }
218
219 /*
220  * setup neighbor hash table
221  */
222 static clib_error_t *
223 lacp_init (vlib_main_t * vm)
224 {
225   clib_error_t *error;
226
227   if ((error = vlib_call_init_function (vm, lacp_periodic_init)))
228     return error;
229
230   return 0;
231 }
232
233 VLIB_INIT_FUNCTION (lacp_init);
234
235 /*
236  * packet trace format function, very similar to
237  * lacp_packet_scan except that we call the per TLV format
238  * functions instead of the per TLV processing functions
239  */
240 u8 *
241 lacp_input_format_trace (u8 * s, va_list * args)
242 {
243   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
244   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
245   lacp_input_trace_t *t = va_arg (*args, lacp_input_trace_t *);
246   lacp_pdu_t *lacpdu = &t->pkt.lacpdu;
247   marker_pdu_t *marker = &t->pkt.marker;
248   int i, len;
249   u8 *p;
250   lacp_state_struct *state_entry;
251
252   s = format (s, "Length: %d\n", t->len);
253   if (t->len >= sizeof (lacp_pdu_t))
254     {
255       switch (lacpdu->subtype)
256         {
257         case MARKER_SUBTYPE:
258           if (marker->version_number == MARKER_PROTOCOL_VERSION)
259             s = format (s, "  Markerv1\n");
260           else
261             s = format (s, "  Subtype %u, Version %u\n", marker->subtype,
262                         marker->version_number);
263           s = format (s, "  Marker Information TLV: type %u\n",
264                       marker->marker_info.tlv_type);
265           s = format (s, "  Marker Information TLV: length %u\n",
266                       marker->marker_info.tlv_length);
267           s = format (s, "  Requester port: %u\n",
268                       marker->marker_info.requester_port);
269           s = format (s, "  Requester system: %U\n", format_ethernet_address,
270                       marker->marker_info.requester_system);
271           s = format (s, "  Requester transaction ID: %u\n",
272                       marker->marker_info.requester_transaction_id);
273           break;
274
275         case LACP_SUBTYPE:
276           if (lacpdu->version_number == LACP_ACTOR_LACP_VERSION)
277             s = format (s, "  LACPv1\n");
278           else
279             s = format (s, "  Subtype %u, Version %u\n", lacpdu->subtype,
280                         lacpdu->version_number);
281           s = format (s, "  Actor Information TLV: length %u\n",
282                       lacpdu->actor.tlv_length);
283           s = format (s, "    System %U\n", format_ethernet_address,
284                       lacpdu->actor.port_info.system);
285           s = format (s, "    System priority %u\n",
286                       ntohs (lacpdu->actor.port_info.system_priority));
287           s = format (s, "    Key %u\n", ntohs (lacpdu->actor.port_info.key));
288           s = format (s, "    Port priority %u\n",
289                       ntohs (lacpdu->actor.port_info.port_priority));
290           s = format (s, "    Port number %u\n",
291                       ntohs (lacpdu->actor.port_info.port_number));
292           s = format (s, "    State 0x%x\n", lacpdu->actor.port_info.state);
293           state_entry = (lacp_state_struct *) & lacp_state_array;
294           while (state_entry->str)
295             {
296               if (lacpdu->actor.port_info.state & (1 << state_entry->bit))
297                 s = format (s, "      %s (%d)\n", state_entry->str,
298                             state_entry->bit);
299               state_entry++;
300             }
301
302           s = format (s, "  Partner Information TLV: length %u\n",
303                       lacpdu->partner.tlv_length);
304           s = format (s, "    System %U\n", format_ethernet_address,
305                       lacpdu->partner.port_info.system);
306           s = format (s, "    System priority %u\n",
307                       ntohs (lacpdu->partner.port_info.system_priority));
308           s =
309             format (s, "    Key %u\n", ntohs (lacpdu->partner.port_info.key));
310           s =
311             format (s, "    Port priority %u\n",
312                     ntohs (lacpdu->partner.port_info.port_priority));
313           s =
314             format (s, "    Port number %u\n",
315                     ntohs (lacpdu->partner.port_info.port_number));
316           s = format (s, "    State 0x%x\n", lacpdu->partner.port_info.state);
317           state_entry = (lacp_state_struct *) & lacp_state_array;
318           while (state_entry->str)
319             {
320               if (lacpdu->partner.port_info.state & (1 << state_entry->bit))
321                 s = format (s, "      %s (%d)\n", state_entry->str,
322                             state_entry->bit);
323               state_entry++;
324             }
325           break;
326
327         default:
328           break;
329         }
330     }
331
332   if (t->len > sizeof (lacp_pdu_t))
333     len = sizeof (lacp_pdu_t);
334   else
335     len = t->len;
336   p = (u8 *) lacpdu;
337   for (i = 0; i < len; i++)
338     {
339       if ((i % 16) == 0)
340         {
341           if (i)
342             s = format (s, "\n");
343           s = format (s, "  0x%04x: ", i);
344         }
345       if ((i % 2) == 0)
346         s = format (s, " ");
347       s = format (s, "%02x", p[i]);
348     }
349
350   return s;
351 }
352
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "gnu")
358  * End:
359  */