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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vnet/bonding/node.h>
18 #include <lacp/node.h>
19 #include <vpp/stats/stat_segment.h>
22 lacp_packet_scan (vlib_main_t * vm, slave_if_t * sif)
24 lacp_pdu_t *lacpdu = (lacp_pdu_t *) sif->last_rx_pkt;
26 if (lacpdu->subtype != LACP_SUBTYPE)
27 return LACP_ERROR_UNSUPPORTED;
30 * According to the spec, no checking on the version number and tlv types.
31 * But we may check the tlv lengths.
33 if ((lacpdu->actor.tlv_length != sizeof (lacp_actor_partner_t)) ||
34 (lacpdu->partner.tlv_length != sizeof (lacp_actor_partner_t)) ||
35 (lacpdu->collector.tlv_length != sizeof (lacp_collector_t)) ||
36 (lacpdu->terminator.tlv_length != 0))
37 return (LACP_ERROR_BAD_TLV);
39 lacp_machine_dispatch (&lacp_rx_machine, vm, sif,
40 LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
42 return LACP_ERROR_NONE;
46 marker_fill_pdu (marker_pdu_t * marker, slave_if_t * sif)
48 marker_pdu_t *pkt = (marker_pdu_t *) sif->last_marker_pkt;
50 marker->marker_info = pkt->marker_info;
51 marker->marker_info.tlv_type = MARKER_RESPONSE_INFORMATION;
55 marker_fill_request_pdu (marker_pdu_t * marker, slave_if_t * sif)
57 marker->marker_info.tlv_type = MARKER_INFORMATION;
58 marker->marker_info.requester_port = sif->actor.port_number;
59 clib_memcpy (marker->marker_info.requester_system, sif->actor.system, 6);
60 marker->marker_info.requester_transaction_id = sif->marker_tx_id;
65 send_ethernet_marker_response_pdu (slave_if_t * sif)
67 lacp_main_t *lm = &lacp_main;
69 ethernet_marker_pdu_t *h0;
70 vnet_hw_interface_t *hw;
74 vlib_main_t *vm = lm->vlib_main;
75 vnet_main_t *vnm = lm->vnet_main;
78 * see lacp_periodic_init() to understand what's already painted
79 * into the buffer by the packet template mechanism
81 h0 = vlib_packet_template_get_packet
82 (vm, &lm->marker_packet_templates[sif->packet_template_index], &bi0);
87 /* Add the interface's ethernet source address */
88 hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
90 clib_memcpy (h0->ethernet.src_address, hw->hw_address,
91 vec_len (hw->hw_address));
93 marker_fill_pdu (&h0->marker, sif);
95 /* Set the outbound packet length */
96 b0 = vlib_get_buffer (vm, bi0);
97 b0->current_length = sizeof (ethernet_marker_pdu_t);
99 b0->total_length_not_including_first_buffer = 0;
101 /* And the outbound interface */
102 vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
104 /* And output the packet on the correct interface */
105 f = vlib_get_frame_to_node (vm, hw->output_node_index);
107 to_next = vlib_frame_vector_args (f);
111 vlib_put_frame_to_node (vm, hw->output_node_index, f);
112 sif->last_marker_pdu_sent_time = vlib_time_now (lm->vlib_main);
113 sif->marker_pdu_sent++;
117 handle_marker_protocol (vlib_main_t * vm, slave_if_t * sif)
119 marker_pdu_t *marker = (marker_pdu_t *) sif->last_marker_pkt;
122 * According to the spec, no checking on the version number and tlv types.
123 * But we may check the tlv lengths.
125 if ((marker->marker_info.tlv_length != sizeof (marker_information_t)) ||
126 (marker->terminator.tlv_length != 0))
127 return (LACP_ERROR_BAD_TLV);
129 send_ethernet_marker_response_pdu (sif);
131 return LACP_ERROR_NONE;
138 lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
140 bond_main_t *bm = &bond_main;
141 lacp_main_t *lm = &lacp_main;
145 marker_pdu_t *marker;
146 uword last_packet_signature;
150 bond_get_slave_by_sw_if_index (vnet_buffer (b0)->sw_if_index[VLIB_RX]);
151 if ((sif == 0) || (sif->mode != BOND_MODE_LACP))
153 return LACP_ERROR_DISABLED;
156 /* Handle marker protocol */
157 marker = (marker_pdu_t *) (b0->data + b0->current_data);
158 if (marker->subtype == MARKER_SUBTYPE)
160 sif->last_marker_pdu_recd_time = vlib_time_now (lm->vlib_main);
161 if (sif->last_marker_pkt)
162 _vec_len (sif->last_marker_pkt) = 0;
163 vec_validate (sif->last_marker_pkt,
164 vlib_buffer_length_in_chain (vm, b0) - 1);
165 nbytes = vlib_buffer_contents (vm, bi0, sif->last_marker_pkt);
166 ASSERT (nbytes <= vec_len (sif->last_marker_pkt));
167 if (nbytes < sizeof (lacp_pdu_t))
169 sif->marker_bad_pdu_received++;
170 return LACP_ERROR_TOO_SMALL;
172 e = handle_marker_protocol (vm, sif);
173 sif->marker_pdu_received++;
178 * typical clib idiom. Don't repeatedly allocate and free
179 * the per-neighbor rx buffer. Reset its apparent length to zero
182 if (sif->last_rx_pkt)
183 _vec_len (sif->last_rx_pkt) = 0;
186 * Make sure the per-neighbor rx buffer is big enough to hold
187 * the data we're about to copy
189 vec_validate (sif->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1);
192 * Coalesce / copy the buffer chain into the per-neighbor
195 nbytes = vlib_buffer_contents (vm, bi0, sif->last_rx_pkt);
196 ASSERT (nbytes <= vec_len (sif->last_rx_pkt));
198 sif->last_lacpdu_recd_time = vlib_time_now (lm->vlib_main);
199 if (nbytes < sizeof (lacp_pdu_t))
201 sif->bad_pdu_received++;
202 return LACP_ERROR_TOO_SMALL;
205 last_packet_signature =
206 hash_memory (sif->last_rx_pkt, vec_len (sif->last_rx_pkt), 0xd00b);
208 if (sif->last_packet_signature_valid &&
209 (sif->last_packet_signature == last_packet_signature) &&
210 ((sif->actor.state & LACP_STEADY_STATE) == LACP_STEADY_STATE))
212 lacp_start_current_while_timer (lm->vlib_main, sif,
213 sif->ttl_in_seconds);
214 e = LACP_ERROR_CACHE_HIT;
218 /* Actually scan the packet */
219 e = lacp_packet_scan (vm, sif);
220 bif = bond_get_master_by_dev_instance (sif->bif_dev_instance);
221 stat_segment_set_state_counter (bm->stats[bif->sw_if_index]
222 [sif->sw_if_index].actor_state,
224 stat_segment_set_state_counter (bm->stats[bif->sw_if_index]
225 [sif->sw_if_index].partner_state,
227 sif->last_packet_signature_valid = 1;
228 sif->last_packet_signature = last_packet_signature;
232 if (sif->last_rx_pkt)
233 _vec_len (sif->last_rx_pkt) = 0;
239 * setup neighbor hash table
241 static clib_error_t *
242 lacp_init (vlib_main_t * vm)
248 VLIB_INIT_FUNCTION (lacp_init) =
250 .runs_after = VLIB_INITS("lacp_periodic_init"),
255 * packet trace format function, very similar to
256 * lacp_packet_scan except that we call the per TLV format
257 * functions instead of the per TLV processing functions
260 lacp_input_format_trace (u8 * s, va_list * args)
262 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
263 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
264 lacp_input_trace_t *t = va_arg (*args, lacp_input_trace_t *);
265 lacp_pdu_t *lacpdu = &t->pkt.lacpdu;
266 marker_pdu_t *marker = &t->pkt.marker;
269 lacp_state_struct *state_entry;
271 s = format (s, "%U:\n", format_vnet_sw_if_index_name, vnet_get_main (),
273 s = format (s, "Length: %d\n", t->len);
274 if (t->len >= sizeof (lacp_pdu_t))
276 switch (lacpdu->subtype)
279 if (marker->version_number == MARKER_PROTOCOL_VERSION)
280 s = format (s, " Markerv1\n");
282 s = format (s, " Subtype %u, Version %u\n", marker->subtype,
283 marker->version_number);
284 s = format (s, " Marker Information TLV: type %u\n",
285 marker->marker_info.tlv_type);
286 s = format (s, " Marker Information TLV: length %u\n",
287 marker->marker_info.tlv_length);
288 s = format (s, " Requester port: %u\n",
289 marker->marker_info.requester_port);
290 s = format (s, " Requester system: %U\n", format_ethernet_address,
291 marker->marker_info.requester_system);
292 s = format (s, " Requester transaction ID: %u\n",
293 marker->marker_info.requester_transaction_id);
297 if (lacpdu->version_number == LACP_ACTOR_LACP_VERSION)
298 s = format (s, " LACPv1\n");
300 s = format (s, " Subtype %u, Version %u\n", lacpdu->subtype,
301 lacpdu->version_number);
302 s = format (s, " Actor Information TLV: length %u\n",
303 lacpdu->actor.tlv_length);
304 s = format (s, " System %U\n", format_ethernet_address,
305 lacpdu->actor.port_info.system);
306 s = format (s, " System priority %u\n",
307 ntohs (lacpdu->actor.port_info.system_priority));
308 s = format (s, " Key %u\n", ntohs (lacpdu->actor.port_info.key));
309 s = format (s, " Port priority %u\n",
310 ntohs (lacpdu->actor.port_info.port_priority));
311 s = format (s, " Port number %u\n",
312 ntohs (lacpdu->actor.port_info.port_number));
313 s = format (s, " State 0x%x\n", lacpdu->actor.port_info.state);
314 state_entry = (lacp_state_struct *) & lacp_state_array;
315 while (state_entry->str)
317 if (lacpdu->actor.port_info.state & (1 << state_entry->bit))
318 s = format (s, " %s (%d)\n", state_entry->str,
323 s = format (s, " Partner Information TLV: length %u\n",
324 lacpdu->partner.tlv_length);
325 s = format (s, " System %U\n", format_ethernet_address,
326 lacpdu->partner.port_info.system);
327 s = format (s, " System priority %u\n",
328 ntohs (lacpdu->partner.port_info.system_priority));
330 format (s, " Key %u\n", ntohs (lacpdu->partner.port_info.key));
332 format (s, " Port priority %u\n",
333 ntohs (lacpdu->partner.port_info.port_priority));
335 format (s, " Port number %u\n",
336 ntohs (lacpdu->partner.port_info.port_number));
337 s = format (s, " State 0x%x\n", lacpdu->partner.port_info.state);
338 state_entry = (lacp_state_struct *) & lacp_state_array;
339 while (state_entry->str)
341 if (lacpdu->partner.port_info.state & (1 << state_entry->bit))
342 s = format (s, " %s (%d)\n", state_entry->str,
353 if (t->len > sizeof (lacp_pdu_t))
354 len = sizeof (lacp_pdu_t);
358 for (i = 0; i < len; i++)
363 s = format (s, "\n");
364 s = format (s, " 0x%04x: ", i);
368 s = format (s, "%02x", p[i]);
375 * fd.io coding-style-patch-verification: ON
378 * eval: (c-set-style "gnu")