FD.io VPP  v21.06-1-gbb7418cf9
Vector Packet Processing
input.c
Go to the documentation of this file.
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 #include <vpp/stats/stat_segment.h>
20 
21 static int
23 {
24  lacp_pdu_t *lacpdu = (lacp_pdu_t *) mif->last_rx_pkt;
25 
26  if (lacpdu->subtype != LACP_SUBTYPE)
27  return LACP_ERROR_UNSUPPORTED;
28 
29  /*
30  * According to the spec, no checking on the version number and tlv types.
31  * But we may check the tlv lengths.
32  */
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);
38 
39  return lacp_machine_dispatch (&lacp_rx_machine, vm, mif,
40  LACP_RX_EVENT_PDU_RECEIVED, &mif->rx_state);
41 }
42 
43 static void
44 marker_fill_pdu (marker_pdu_t * marker, member_if_t * mif)
45 {
46  marker_pdu_t *pkt = (marker_pdu_t *) mif->last_marker_pkt;
47 
48  marker->marker_info = pkt->marker_info;
49  marker->marker_info.tlv_type = MARKER_RESPONSE_INFORMATION;
50 }
51 
52 void
53 marker_fill_request_pdu (marker_pdu_t * marker, member_if_t * mif)
54 {
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;
59  mif->marker_tx_id++;
60 }
61 
62 static void
64 {
65  lacp_main_t *lm = &lacp_main;
66  u32 *to_next;
67  ethernet_marker_pdu_t *h0;
69  u32 bi0;
70  vlib_buffer_t *b0;
71  vlib_frame_t *f;
72  vnet_main_t *vnm = lm->vnet_main;
73 
74  /*
75  * see lacp_periodic_init() to understand what's already painted
76  * into the buffer by the packet template mechanism
77  */
79  (vm, &lm->marker_packet_templates[mif->packet_template_index], &bi0);
80 
81  if (!h0)
82  return;
83 
84  /* Add the interface's ethernet source address */
85  hw = vnet_get_sup_hw_interface (vnm, mif->sw_if_index);
86 
87  clib_memcpy (h0->ethernet.src_address, hw->hw_address,
88  vec_len (hw->hw_address));
89 
90  marker_fill_pdu (&h0->marker, mif);
91 
92  /* Set the outbound packet length */
93  b0 = vlib_get_buffer (vm, bi0);
94  b0->current_length = sizeof (ethernet_marker_pdu_t);
95  b0->current_data = 0;
97 
98  /* And the outbound interface */
99  vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
100 
101  /* And output the packet on the correct interface */
103 
104  to_next = vlib_frame_vector_args (f);
105  to_next[0] = bi0;
106  f->n_vectors = 1;
107 
110  mif->marker_pdu_sent++;
111 }
112 
113 static int
115 {
116  marker_pdu_t *marker = (marker_pdu_t *) mif->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 
127 
128  return LACP_ERROR_NONE;
129 }
130 
131 /*
132  * lacp input routine
133  */
136 {
137  bond_main_t *bm = &bond_main;
138  member_if_t *mif;
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  mif =
147  if ((mif == 0) || (mif->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  {
157  if (mif->last_marker_pkt)
158  _vec_len (mif->last_marker_pkt) = 0;
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))
164  {
166  return LACP_ERROR_TOO_SMALL;
167  }
168  e = handle_marker_protocol (vm, mif);
169  mif->marker_pdu_received++;
170  return e;
171  }
172 
173  /*
174  * typical clib idiom. Don't repeatedly allocate and free
175  * the per-neighbor rx buffer. Reset its apparent length to zero
176  * and reuse it.
177  */
178  if (mif->last_rx_pkt)
179  _vec_len (mif->last_rx_pkt) = 0;
180 
181  /*
182  * Make sure the per-neighbor rx buffer is big enough to hold
183  * the data we're about to copy
184  */
186 
187  /*
188  * Coalesce / copy the buffer chain into the per-neighbor
189  * rx buffer
190  */
191  nbytes = vlib_buffer_contents (vm, bi0, mif->last_rx_pkt);
192  ASSERT (nbytes <= vec_len (mif->last_rx_pkt));
193 
195  if (nbytes < sizeof (lacp_pdu_t))
196  {
197  mif->bad_pdu_received++;
198  return LACP_ERROR_TOO_SMALL;
199  }
200 
201  last_packet_signature =
202  hash_memory (mif->last_rx_pkt, vec_len (mif->last_rx_pkt), 0xd00b);
203 
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))
207  {
209  e = LACP_ERROR_CACHE_HIT;
210  }
211  else
212  {
213  /* Actually scan the packet */
214  e = lacp_packet_scan (vm, mif);
217  [mif->sw_if_index].actor_state,
218  mif->actor.state);
220  [mif->sw_if_index].partner_state,
221  mif->partner.state);
223  mif->last_packet_signature = last_packet_signature;
224  }
225  mif->pdu_received++;
226 
227  if (mif->last_rx_pkt)
228  _vec_len (mif->last_rx_pkt) = 0;
229 
230  return e;
231 }
232 
233 /*
234  * setup neighbor hash table
235  */
236 static clib_error_t *
238 {
239  return 0;
240 }
241 
242 /* *INDENT-OFF* */
244 {
245  .runs_after = VLIB_INITS("lacp_periodic_init"),
246 };
247 /* *INDENT-ON* */
248 
249 /*
250  * packet trace format function, very similar to
251  * lacp_packet_scan except that we call the per TLV format
252  * functions instead of the per TLV processing functions
253  */
254 u8 *
255 lacp_input_format_trace (u8 * s, va_list * args)
256 {
257  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
258  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
259  lacp_input_trace_t *t = va_arg (*args, lacp_input_trace_t *);
260  lacp_pdu_t *lacpdu = &t->pkt.lacpdu;
261  marker_pdu_t *marker = &t->pkt.marker;
262  int i, len;
263  u8 *p;
264  lacp_state_struct *state_entry;
265 
267  t->sw_if_index);
268  s = format (s, "Length: %d\n", t->len);
269  if (t->len >= sizeof (lacp_pdu_t))
270  {
271  switch (lacpdu->subtype)
272  {
273  case MARKER_SUBTYPE:
274  if (marker->version_number == MARKER_PROTOCOL_VERSION)
275  s = format (s, " Markerv1\n");
276  else
277  s = format (s, " Subtype %u, Version %u\n", marker->subtype,
278  marker->version_number);
279  s = format (s, " Marker Information TLV: type %u\n",
280  marker->marker_info.tlv_type);
281  s = format (s, " Marker Information TLV: length %u\n",
282  marker->marker_info.tlv_length);
283  s = format (s, " Requester port: %u\n",
284  ntohs (marker->marker_info.requester_port));
285  s = format (s, " Requester system: %U\n", format_ethernet_address,
286  marker->marker_info.requester_system);
287  s = format (s, " Requester transaction ID: %u\n",
288  ntohl (marker->marker_info.requester_transaction_id));
289  break;
290 
291  case LACP_SUBTYPE:
292  if (lacpdu->version_number == LACP_ACTOR_LACP_VERSION)
293  s = format (s, " LACPv1\n");
294  else
295  s = format (s, " Subtype %u, Version %u\n", lacpdu->subtype,
296  lacpdu->version_number);
297  s = format (s, " Actor Information TLV: length %u\n",
298  lacpdu->actor.tlv_length);
299  s = format (s, " System %U\n", format_ethernet_address,
300  lacpdu->actor.port_info.system);
301  s = format (s, " System priority %u\n",
302  ntohs (lacpdu->actor.port_info.system_priority));
303  s = format (s, " Key %u\n", ntohs (lacpdu->actor.port_info.key));
304  s = format (s, " Port priority %u\n",
305  ntohs (lacpdu->actor.port_info.port_priority));
306  s = format (s, " Port number %u\n",
307  ntohs (lacpdu->actor.port_info.port_number));
308  s = format (s, " State 0x%x\n", lacpdu->actor.port_info.state);
309  state_entry = (lacp_state_struct *) & lacp_state_array;
310  while (state_entry->str)
311  {
312  if (lacpdu->actor.port_info.state & (1 << state_entry->bit))
313  s = format (s, " %s (%d)\n", state_entry->str,
314  state_entry->bit);
315  state_entry++;
316  }
317 
318  s = format (s, " Partner Information TLV: length %u\n",
319  lacpdu->partner.tlv_length);
320  s = format (s, " System %U\n", format_ethernet_address,
321  lacpdu->partner.port_info.system);
322  s = format (s, " System priority %u\n",
323  ntohs (lacpdu->partner.port_info.system_priority));
324  s =
325  format (s, " Key %u\n", ntohs (lacpdu->partner.port_info.key));
326  s =
327  format (s, " Port priority %u\n",
328  ntohs (lacpdu->partner.port_info.port_priority));
329  s =
330  format (s, " Port number %u\n",
331  ntohs (lacpdu->partner.port_info.port_number));
332  s = format (s, " State 0x%x\n", lacpdu->partner.port_info.state);
333  state_entry = (lacp_state_struct *) & lacp_state_array;
334  while (state_entry->str)
335  {
336  if (lacpdu->partner.port_info.state & (1 << state_entry->bit))
337  s = format (s, " %s (%d)\n", state_entry->str,
338  state_entry->bit);
339  state_entry++;
340  }
341  break;
342 
343  default:
344  break;
345  }
346  }
347 
348  if (t->len > sizeof (lacp_pdu_t))
349  len = sizeof (lacp_pdu_t);
350  else
351  len = t->len;
352  p = (u8 *) lacpdu;
353  for (i = 0; i < len; i++)
354  {
355  if ((i % 16) == 0)
356  {
357  if (i)
358  s = format (s, "\n");
359  s = format (s, " 0x%04x: ", i);
360  }
361  if ((i % 2) == 0)
362  s = format (s, " ");
363  s = format (s, "%02x", p[i]);
364  }
365 
366  return s;
367 }
368 
369 /*
370  * fd.io coding-style-patch-verification: ON
371  *
372  * Local Variables:
373  * eval: (c-set-style "gnu")
374  * End:
375  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:524
#define CLIB_UNUSED(x)
Definition: clib.h:90
lacp_stats_t ** stats
Definition: node.h:388
u64 marker_bad_pdu_received
Definition: node.h:343
u64 marker_pdu_sent
Definition: node.h:346
#define ntohs(x)
Definition: af_xdp.bpf.c:29
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
u32 marker_tx_id
Definition: node.h:321
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:119
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
static bond_if_t * bond_get_bond_if_by_dev_instance(u32 dev_instance)
Definition: node.h:517
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
union lacp_input_trace_t::@60 pkt
unsigned int u32
Definition: types.h:88
#define clib_memcpy(d, s, n)
Definition: string.h:197
vlib_frame_t * f
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
u8 * lacp_input_format_trace(u8 *s, va_list *args)
Definition: input.c:255
marker_pdu_t marker
Definition: node.h:88
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
void * vlib_packet_template_get_packet(vlib_main_t *vm, vlib_packet_template_t *t, u32 *bi_result)
Definition: buffer.c:377
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vnet_main_t * vnet_main
Definition: node.h:124
f64 last_lacpdu_recd_time
Definition: node.h:295
u32 sw_if_index
Definition: node.h:184
description fragment has unexpected format
Definition: map.api:433
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:184
vnet_main_t * vnet_get_main(void)
u64 pdu_received
Definition: node.h:331
static int handle_marker_protocol(vlib_main_t *vm, member_if_t *mif)
Definition: input.c:114
#define MARKER_SUBTYPE
Definition: protocol.h:132
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
u8 mode
Definition: node.h:328
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:218
#define LACP_SUBTYPE
Definition: protocol.h:25
lacp_port_info_t partner
Definition: node.h:254
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
#define MARKER_PROTOCOL_VERSION
Definition: protocol.h:133
u8 last_packet_signature_valid
Definition: node.h:235
static int lacp_packet_scan(vlib_main_t *vm, member_if_t *mif)
Definition: input.c:22
static uword vlib_buffer_contents(vlib_main_t *vm, u32 buffer_index, u8 *contents)
Copy buffer contents to memory.
Definition: buffer_funcs.h:467
u8 len
Definition: ip_types.api:103
u32 partner_state
Definition: node.h:357
u16 n_vectors
Definition: node.h:388
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
lacp_error_t
Definition: node.h:71
f32 ttl_in_seconds
Definition: node.h:221
u8 data[]
Packet data.
Definition: buffer.h:204
static void lacp_start_current_while_timer(vlib_main_t *vm, member_if_t *mif, u8 expiration)
Definition: rx_machine.h:77
uword last_packet_signature
Definition: node.h:236
lacp_main_t lacp_main
Definition: lacp.c:26
__clib_export uword hash_memory(void *p, word n_bytes, uword state)
Definition: hash.c:275
f64 last_marker_pdu_sent_time
Definition: node.h:298
u8 packet_template_index
Definition: node.h:230
static void send_ethernet_marker_response_pdu(vlib_main_t *vm, member_if_t *mif)
Definition: input.c:63
#define ASSERT(truth)
lacp_pdu_t lacpdu
Definition: node.h:89
lacp_error_t lacp_input(vlib_main_t *vm, vlib_buffer_t *b0, u32 bi0)
Definition: input.c:135
bond_main_t bond_main
Definition: node.c:25
static member_if_t * bond_get_member_by_sw_if_index(u32 sw_if_index)
Definition: node.h:525
vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES]
Definition: node.h:133
int rx_state
Definition: node.h:313
void marker_fill_request_pdu(marker_pdu_t *marker, member_if_t *mif)
Definition: input.c:53
Definition: defs.h:47
lacp_machine_t lacp_rx_machine
Definition: rx_machine.c:108
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
u32 sw_if_index
Definition: node.h:84
VLIB buffer representation.
Definition: buffer.h:111
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
static void marker_fill_pdu(marker_pdu_t *marker, member_if_t *mif)
Definition: input.c:44
#define LACP_STEADY_STATE
Definition: protocol.h:64
int lacp_machine_dispatch(lacp_machine_t *machine, vlib_main_t *vm, member_if_t *mif, int event, int *state)
Definition: lacp.c:317
#define vnet_buffer(b)
Definition: buffer.h:437
u32 sw_if_index
Definition: node.h:218
static clib_error_t * lacp_init(vlib_main_t *vm)
Definition: input.c:237
u32 actor_state
Definition: node.h:358
u64 bad_pdu_received
Definition: node.h:334
f64 last_marker_pdu_recd_time
Definition: node.h:301
lacp_port_info_t actor
Definition: node.h:255
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:176
u8 * last_marker_pkt
Definition: node.h:242
lacp_state_struct lacp_state_array[]
Definition: node.c:21
#define VLIB_INITS(...)
Definition: init.h:352
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
Definition: defs.h:46
u8 * last_rx_pkt
Definition: node.h:239
void stat_segment_set_state_counter(u32 index, u64 value)
Definition: stat_segment.c:962
#define LACP_ACTOR_LACP_VERSION
Definition: protocol.h:26
u64 marker_pdu_received
Definition: node.h:340
u32 bif_dev_instance
Definition: node.h:323