lacp: add actor steady state check prior to skip processing lacp pdu
[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   sif->last_marker_pdu_sent_time = vlib_time_now (lm->vlib_main);
112   sif->marker_pdu_sent++;
113 }
114
115 static int
116 handle_marker_protocol (vlib_main_t * vm, slave_if_t * sif)
117 {
118   marker_pdu_t *marker = (marker_pdu_t *) sif->last_marker_pkt;
119
120   /*
121    * According to the spec, no checking on the version number and tlv types.
122    * But we may check the tlv lengths.
123    */
124   if ((marker->marker_info.tlv_length != sizeof (marker_information_t)) ||
125       (marker->terminator.tlv_length != 0))
126     return (LACP_ERROR_BAD_TLV);
127
128   send_ethernet_marker_response_pdu (sif);
129
130   return LACP_ERROR_NONE;
131 }
132
133 /*
134  * lacp input routine
135  */
136 lacp_error_t
137 lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
138 {
139   lacp_main_t *lm = &lacp_main;
140   slave_if_t *sif;
141   uword nbytes;
142   lacp_error_t e;
143   marker_pdu_t *marker;
144   uword last_packet_signature;
145
146   sif =
147     bond_get_slave_by_sw_if_index (vnet_buffer (b0)->sw_if_index[VLIB_RX]);
148   if ((sif == 0) || (sif->mode != BOND_MODE_LACP))
149     {
150       return LACP_ERROR_DISABLED;
151     }
152
153   /* Handle marker protocol */
154   marker = (marker_pdu_t *) (b0->data + b0->current_data);
155   if (marker->subtype == MARKER_SUBTYPE)
156     {
157       sif->last_marker_pdu_recd_time = vlib_time_now (lm->vlib_main);
158       if (sif->last_marker_pkt)
159         _vec_len (sif->last_marker_pkt) = 0;
160       vec_validate (sif->last_marker_pkt,
161                     vlib_buffer_length_in_chain (vm, b0) - 1);
162       nbytes = vlib_buffer_contents (vm, bi0, sif->last_marker_pkt);
163       ASSERT (nbytes <= vec_len (sif->last_marker_pkt));
164       if (nbytes < sizeof (lacp_pdu_t))
165         {
166           sif->marker_bad_pdu_received++;
167           return LACP_ERROR_TOO_SMALL;
168         }
169       e = handle_marker_protocol (vm, sif);
170       sif->marker_pdu_received++;
171       return e;
172     }
173
174   /*
175    * typical clib idiom. Don't repeatedly allocate and free
176    * the per-neighbor rx buffer. Reset its apparent length to zero
177    * and reuse it.
178    */
179   if (sif->last_rx_pkt)
180     _vec_len (sif->last_rx_pkt) = 0;
181
182   /*
183    * Make sure the per-neighbor rx buffer is big enough to hold
184    * the data we're about to copy
185    */
186   vec_validate (sif->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1);
187
188   /*
189    * Coalesce / copy the buffer chain into the per-neighbor
190    * rx buffer
191    */
192   nbytes = vlib_buffer_contents (vm, bi0, sif->last_rx_pkt);
193   ASSERT (nbytes <= vec_len (sif->last_rx_pkt));
194
195   sif->last_lacpdu_recd_time = vlib_time_now (lm->vlib_main);
196   if (nbytes < sizeof (lacp_pdu_t))
197     {
198       sif->bad_pdu_received++;
199       return LACP_ERROR_TOO_SMALL;
200     }
201
202   last_packet_signature =
203     hash_memory (sif->last_rx_pkt, vec_len (sif->last_rx_pkt), 0xd00b);
204
205   if (sif->last_packet_signature_valid &&
206       (sif->last_packet_signature == last_packet_signature) &&
207       ((sif->actor.state & LACP_STEADY_STATE) == LACP_STEADY_STATE))
208     {
209       lacp_start_current_while_timer (lm->vlib_main, sif,
210                                       sif->ttl_in_seconds);
211       e = LACP_ERROR_CACHE_HIT;
212     }
213   else
214     {
215       /* Actually scan the packet */
216       e = lacp_packet_scan (vm, sif);
217       sif->last_packet_signature_valid = 1;
218       sif->last_packet_signature = last_packet_signature;
219     }
220   sif->pdu_received++;
221
222   if (sif->last_rx_pkt)
223     _vec_len (sif->last_rx_pkt) = 0;
224
225   return e;
226 }
227
228 /*
229  * setup neighbor hash table
230  */
231 static clib_error_t *
232 lacp_init (vlib_main_t * vm)
233 {
234   return 0;
235 }
236
237 /* *INDENT-OFF* */
238 VLIB_INIT_FUNCTION (lacp_init) =
239 {
240   .runs_after = VLIB_INITS("lacp_periodic_init"),
241 };
242 /* *INDENT-ON* */
243
244 /*
245  * packet trace format function, very similar to
246  * lacp_packet_scan except that we call the per TLV format
247  * functions instead of the per TLV processing functions
248  */
249 u8 *
250 lacp_input_format_trace (u8 * s, va_list * args)
251 {
252   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
253   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
254   lacp_input_trace_t *t = va_arg (*args, lacp_input_trace_t *);
255   lacp_pdu_t *lacpdu = &t->pkt.lacpdu;
256   marker_pdu_t *marker = &t->pkt.marker;
257   int i, len;
258   u8 *p;
259   lacp_state_struct *state_entry;
260
261   s = format (s, "%U:\n", format_vnet_sw_if_index_name, vnet_get_main (),
262               t->sw_if_index);
263   s = format (s, "Length: %d\n", t->len);
264   if (t->len >= sizeof (lacp_pdu_t))
265     {
266       switch (lacpdu->subtype)
267         {
268         case MARKER_SUBTYPE:
269           if (marker->version_number == MARKER_PROTOCOL_VERSION)
270             s = format (s, "  Markerv1\n");
271           else
272             s = format (s, "  Subtype %u, Version %u\n", marker->subtype,
273                         marker->version_number);
274           s = format (s, "  Marker Information TLV: type %u\n",
275                       marker->marker_info.tlv_type);
276           s = format (s, "  Marker Information TLV: length %u\n",
277                       marker->marker_info.tlv_length);
278           s = format (s, "  Requester port: %u\n",
279                       marker->marker_info.requester_port);
280           s = format (s, "  Requester system: %U\n", format_ethernet_address,
281                       marker->marker_info.requester_system);
282           s = format (s, "  Requester transaction ID: %u\n",
283                       marker->marker_info.requester_transaction_id);
284           break;
285
286         case LACP_SUBTYPE:
287           if (lacpdu->version_number == LACP_ACTOR_LACP_VERSION)
288             s = format (s, "  LACPv1\n");
289           else
290             s = format (s, "  Subtype %u, Version %u\n", lacpdu->subtype,
291                         lacpdu->version_number);
292           s = format (s, "  Actor Information TLV: length %u\n",
293                       lacpdu->actor.tlv_length);
294           s = format (s, "    System %U\n", format_ethernet_address,
295                       lacpdu->actor.port_info.system);
296           s = format (s, "    System priority %u\n",
297                       ntohs (lacpdu->actor.port_info.system_priority));
298           s = format (s, "    Key %u\n", ntohs (lacpdu->actor.port_info.key));
299           s = format (s, "    Port priority %u\n",
300                       ntohs (lacpdu->actor.port_info.port_priority));
301           s = format (s, "    Port number %u\n",
302                       ntohs (lacpdu->actor.port_info.port_number));
303           s = format (s, "    State 0x%x\n", lacpdu->actor.port_info.state);
304           state_entry = (lacp_state_struct *) & lacp_state_array;
305           while (state_entry->str)
306             {
307               if (lacpdu->actor.port_info.state & (1 << state_entry->bit))
308                 s = format (s, "      %s (%d)\n", state_entry->str,
309                             state_entry->bit);
310               state_entry++;
311             }
312
313           s = format (s, "  Partner Information TLV: length %u\n",
314                       lacpdu->partner.tlv_length);
315           s = format (s, "    System %U\n", format_ethernet_address,
316                       lacpdu->partner.port_info.system);
317           s = format (s, "    System priority %u\n",
318                       ntohs (lacpdu->partner.port_info.system_priority));
319           s =
320             format (s, "    Key %u\n", ntohs (lacpdu->partner.port_info.key));
321           s =
322             format (s, "    Port priority %u\n",
323                     ntohs (lacpdu->partner.port_info.port_priority));
324           s =
325             format (s, "    Port number %u\n",
326                     ntohs (lacpdu->partner.port_info.port_number));
327           s = format (s, "    State 0x%x\n", lacpdu->partner.port_info.state);
328           state_entry = (lacp_state_struct *) & lacp_state_array;
329           while (state_entry->str)
330             {
331               if (lacpdu->partner.port_info.state & (1 << state_entry->bit))
332                 s = format (s, "      %s (%d)\n", state_entry->str,
333                             state_entry->bit);
334               state_entry++;
335             }
336           break;
337
338         default:
339           break;
340         }
341     }
342
343   if (t->len > sizeof (lacp_pdu_t))
344     len = sizeof (lacp_pdu_t);
345   else
346     len = t->len;
347   p = (u8 *) lacpdu;
348   for (i = 0; i < len; i++)
349     {
350       if ((i % 16) == 0)
351         {
352           if (i)
353             s = format (s, "\n");
354           s = format (s, "  0x%04x: ", i);
355         }
356       if ((i % 2) == 0)
357         s = format (s, " ");
358       s = format (s, "%02x", p[i]);
359     }
360
361   return s;
362 }
363
364 /*
365  * fd.io coding-style-patch-verification: ON
366  *
367  * Local Variables:
368  * eval: (c-set-style "gnu")
369  * End:
370  */