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 <vlib/stats/stats.h>
22 lacp_packet_scan (vlib_main_t * vm, member_if_t * mif)
24 lacp_pdu_t *lacpdu = (lacp_pdu_t *) mif->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 return lacp_machine_dispatch (&lacp_rx_machine, vm, mif,
40 LACP_RX_EVENT_PDU_RECEIVED, &mif->rx_state);
44 marker_fill_pdu (marker_pdu_t * marker, member_if_t * mif)
46 marker_pdu_t *pkt = (marker_pdu_t *) mif->last_marker_pkt;
48 marker->marker_info = pkt->marker_info;
49 marker->marker_info.tlv_type = MARKER_RESPONSE_INFORMATION;
53 marker_fill_request_pdu (marker_pdu_t * marker, member_if_t * mif)
55 marker->marker_info.tlv_type = MARKER_INFORMATION;
56 marker->marker_info.requester_port = mif->actor.port_number;
57 clib_memcpy (marker->marker_info.requester_system, mif->actor.system, 6);
58 marker->marker_info.requester_transaction_id = mif->marker_tx_id;
63 send_ethernet_marker_response_pdu (vlib_main_t * vm, member_if_t * mif)
65 lacp_main_t *lm = &lacp_main;
67 ethernet_marker_pdu_t *h0;
68 vnet_hw_interface_t *hw;
72 vnet_main_t *vnm = lm->vnet_main;
75 * see lacp_periodic_init() to understand what's already painted
76 * into the buffer by the packet template mechanism
78 h0 = vlib_packet_template_get_packet
79 (vm, &lm->marker_packet_templates[mif->packet_template_index], &bi0);
84 /* Add the interface's ethernet source address */
85 hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
87 clib_memcpy (h0->ethernet.src_address, hw->hw_address,
88 vec_len (hw->hw_address));
90 marker_fill_pdu (&h0->marker, mif);
92 /* Set the outbound packet length */
93 b0 = vlib_get_buffer (vm, bi0);
94 b0->current_length = sizeof (ethernet_marker_pdu_t);
96 b0->total_length_not_including_first_buffer = 0;
98 /* And the outbound interface */
99 vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
101 /* And output the packet on the correct interface */
102 f = vlib_get_frame_to_node (vm, hw->output_node_index);
104 to_next = vlib_frame_vector_args (f);
108 vlib_put_frame_to_node (vm, hw->output_node_index, f);
109 mif->last_marker_pdu_sent_time = vlib_time_now (vm);
110 mif->marker_pdu_sent++;
114 handle_marker_protocol (vlib_main_t * vm, member_if_t * mif)
116 marker_pdu_t *marker = (marker_pdu_t *) mif->last_marker_pkt;
119 * According to the spec, no checking on the version number and tlv types.
120 * But we may check the tlv lengths.
122 if ((marker->marker_info.tlv_length != sizeof (marker_information_t)) ||
123 (marker->terminator.tlv_length != 0))
124 return (LACP_ERROR_BAD_TLV);
126 send_ethernet_marker_response_pdu (vm, mif);
128 return LACP_ERROR_NONE;
135 lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0)
137 bond_main_t *bm = &bond_main;
141 marker_pdu_t *marker;
142 uword last_packet_signature;
146 bond_get_member_by_sw_if_index (vnet_buffer (b0)->sw_if_index[VLIB_RX]);
147 if ((mif == 0) || (mif->mode != BOND_MODE_LACP))
149 return LACP_ERROR_DISABLED;
152 /* Handle marker protocol */
153 marker = (marker_pdu_t *) (b0->data + b0->current_data);
154 if (marker->subtype == MARKER_SUBTYPE)
156 mif->last_marker_pdu_recd_time = vlib_time_now (vm);
157 if (mif->last_marker_pkt)
158 vec_set_len (mif->last_marker_pkt, 0);
159 vec_validate (mif->last_marker_pkt,
160 vlib_buffer_length_in_chain (vm, b0) - 1);
161 nbytes = vlib_buffer_contents (vm, bi0, mif->last_marker_pkt);
162 ASSERT (nbytes <= vec_len (mif->last_marker_pkt));
163 if (nbytes < sizeof (lacp_pdu_t))
165 mif->marker_bad_pdu_received++;
166 return LACP_ERROR_TOO_SMALL;
168 e = handle_marker_protocol (vm, mif);
169 mif->marker_pdu_received++;
174 * typical clib idiom. Don't repeatedly allocate and free
175 * the per-neighbor rx buffer. Reset its apparent length to zero
178 if (mif->last_rx_pkt)
179 vec_set_len (mif->last_rx_pkt, 0);
182 * Make sure the per-neighbor rx buffer is big enough to hold
183 * the data we're about to copy
185 vec_validate (mif->last_rx_pkt, vlib_buffer_length_in_chain (vm, b0) - 1);
188 * Coalesce / copy the buffer chain into the per-neighbor
191 nbytes = vlib_buffer_contents (vm, bi0, mif->last_rx_pkt);
192 ASSERT (nbytes <= vec_len (mif->last_rx_pkt));
194 mif->last_lacpdu_recd_time = vlib_time_now (vm);
195 if (nbytes < sizeof (lacp_pdu_t))
197 mif->bad_pdu_received++;
198 return LACP_ERROR_TOO_SMALL;
201 last_packet_signature =
202 hash_memory (mif->last_rx_pkt, vec_len (mif->last_rx_pkt), 0xd00b);
204 if (mif->last_packet_signature_valid &&
205 (mif->last_packet_signature == last_packet_signature) &&
206 ((mif->actor.state & LACP_STEADY_STATE) == LACP_STEADY_STATE))
208 lacp_start_current_while_timer (vm, mif, mif->ttl_in_seconds);
209 e = LACP_ERROR_CACHE_HIT;
213 /* Actually scan the packet */
214 e = lacp_packet_scan (vm, mif);
215 bif = bond_get_bond_if_by_dev_instance (mif->bif_dev_instance);
216 vlib_stats_set_gauge (
217 bm->stats[bif->sw_if_index][mif->sw_if_index].actor_state,
219 vlib_stats_set_gauge (
220 bm->stats[bif->sw_if_index][mif->sw_if_index].partner_state,
222 mif->last_packet_signature_valid = 1;
223 mif->last_packet_signature = last_packet_signature;
227 if (mif->last_rx_pkt)
228 vec_set_len (mif->last_rx_pkt, 0);
234 * setup neighbor hash table
236 static clib_error_t *
237 lacp_init (vlib_main_t * vm)
242 VLIB_INIT_FUNCTION (lacp_init) =
244 .runs_after = VLIB_INITS("lacp_periodic_init"),
248 * packet trace format function, very similar to
249 * lacp_packet_scan except that we call the per TLV format
250 * functions instead of the per TLV processing functions
253 lacp_input_format_trace (u8 * s, va_list * args)
255 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
256 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
257 lacp_input_trace_t *t = va_arg (*args, lacp_input_trace_t *);
258 lacp_pdu_t *lacpdu = &t->pkt.lacpdu;
259 marker_pdu_t *marker = &t->pkt.marker;
262 lacp_state_struct *state_entry;
264 s = format (s, "%U:\n", format_vnet_sw_if_index_name, vnet_get_main (),
266 s = format (s, "Length: %d\n", t->len);
267 if (t->len >= sizeof (lacp_pdu_t))
269 switch (lacpdu->subtype)
272 if (marker->version_number == MARKER_PROTOCOL_VERSION)
273 s = format (s, " Markerv1\n");
275 s = format (s, " Subtype %u, Version %u\n", marker->subtype,
276 marker->version_number);
277 s = format (s, " Marker Information TLV: type %u\n",
278 marker->marker_info.tlv_type);
279 s = format (s, " Marker Information TLV: length %u\n",
280 marker->marker_info.tlv_length);
281 s = format (s, " Requester port: %u\n",
282 ntohs (marker->marker_info.requester_port));
283 s = format (s, " Requester system: %U\n", format_ethernet_address,
284 marker->marker_info.requester_system);
285 s = format (s, " Requester transaction ID: %u\n",
286 ntohl (marker->marker_info.requester_transaction_id));
290 if (lacpdu->version_number == LACP_ACTOR_LACP_VERSION)
291 s = format (s, " LACPv1\n");
293 s = format (s, " Subtype %u, Version %u\n", lacpdu->subtype,
294 lacpdu->version_number);
295 s = format (s, " Actor Information TLV: length %u\n",
296 lacpdu->actor.tlv_length);
297 s = format (s, " System %U\n", format_ethernet_address,
298 lacpdu->actor.port_info.system);
299 s = format (s, " System priority %u\n",
300 ntohs (lacpdu->actor.port_info.system_priority));
301 s = format (s, " Key %u\n", ntohs (lacpdu->actor.port_info.key));
302 s = format (s, " Port priority %u\n",
303 ntohs (lacpdu->actor.port_info.port_priority));
304 s = format (s, " Port number %u\n",
305 ntohs (lacpdu->actor.port_info.port_number));
306 s = format (s, " State 0x%x\n", lacpdu->actor.port_info.state);
307 state_entry = (lacp_state_struct *) & lacp_state_array;
308 while (state_entry->str)
310 if (lacpdu->actor.port_info.state & (1 << state_entry->bit))
311 s = format (s, " %s (%d)\n", state_entry->str,
316 s = format (s, " Partner Information TLV: length %u\n",
317 lacpdu->partner.tlv_length);
318 s = format (s, " System %U\n", format_ethernet_address,
319 lacpdu->partner.port_info.system);
320 s = format (s, " System priority %u\n",
321 ntohs (lacpdu->partner.port_info.system_priority));
323 format (s, " Key %u\n", ntohs (lacpdu->partner.port_info.key));
325 format (s, " Port priority %u\n",
326 ntohs (lacpdu->partner.port_info.port_priority));
328 format (s, " Port number %u\n",
329 ntohs (lacpdu->partner.port_info.port_number));
330 s = format (s, " State 0x%x\n", lacpdu->partner.port_info.state);
331 state_entry = (lacp_state_struct *) & lacp_state_array;
332 while (state_entry->str)
334 if (lacpdu->partner.port_info.state & (1 << state_entry->bit))
335 s = format (s, " %s (%d)\n", state_entry->str,
346 if (t->len > sizeof (lacp_pdu_t))
347 len = sizeof (lacp_pdu_t);
351 for (i = 0; i < len; i++)
356 s = format (s, "\n");
357 s = format (s, " 0x%04x: ", i);
361 s = format (s, "%02x", p[i]);
368 * fd.io coding-style-patch-verification: ON
371 * eval: (c-set-style "gnu")