FD.io VPP  v20.01-48-g3e0dafb74
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 *) sif->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 
40  LACP_RX_EVENT_PDU_RECEIVED, &sif->rx_state);
41 
42  return LACP_ERROR_NONE;
43 }
44 
45 static void
46 marker_fill_pdu (marker_pdu_t * marker, slave_if_t * sif)
47 {
48  marker_pdu_t *pkt = (marker_pdu_t *) sif->last_marker_pkt;
49 
50  marker->marker_info = pkt->marker_info;
51  marker->marker_info.tlv_type = MARKER_RESPONSE_INFORMATION;
52 }
53 
54 void
55 marker_fill_request_pdu (marker_pdu_t * marker, slave_if_t * sif)
56 {
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;
61  sif->marker_tx_id++;
62 }
63 
64 static void
66 {
67  lacp_main_t *lm = &lacp_main;
68  u32 *to_next;
69  ethernet_marker_pdu_t *h0;
71  u32 bi0;
72  vlib_buffer_t *b0;
73  vlib_frame_t *f;
74  vlib_main_t *vm = lm->vlib_main;
75  vnet_main_t *vnm = lm->vnet_main;
76 
77  /*
78  * see lacp_periodic_init() to understand what's already painted
79  * into the buffer by the packet template mechanism
80  */
82  (vm, &lm->marker_packet_templates[sif->packet_template_index], &bi0);
83 
84  if (!h0)
85  return;
86 
87  /* Add the interface's ethernet source address */
88  hw = vnet_get_sup_hw_interface (vnm, sif->sw_if_index);
89 
90  clib_memcpy (h0->ethernet.src_address, hw->hw_address,
91  vec_len (hw->hw_address));
92 
93  marker_fill_pdu (&h0->marker, sif);
94 
95  /* Set the outbound packet length */
96  b0 = vlib_get_buffer (vm, bi0);
97  b0->current_length = sizeof (ethernet_marker_pdu_t);
98  b0->current_data = 0;
100 
101  /* And the outbound interface */
102  vnet_buffer (b0)->sw_if_index[VLIB_TX] = hw->sw_if_index;
103 
104  /* And output the packet on the correct interface */
106 
107  to_next = vlib_frame_vector_args (f);
108  to_next[0] = bi0;
109  f->n_vectors = 1;
110 
113  sif->marker_pdu_sent++;
114 }
115 
116 static int
118 {
119  marker_pdu_t *marker = (marker_pdu_t *) sif->last_marker_pkt;
120 
121  /*
122  * According to the spec, no checking on the version number and tlv types.
123  * But we may check the tlv lengths.
124  */
125  if ((marker->marker_info.tlv_length != sizeof (marker_information_t)) ||
126  (marker->terminator.tlv_length != 0))
127  return (LACP_ERROR_BAD_TLV);
128 
130 
131  return LACP_ERROR_NONE;
132 }
133 
134 /*
135  * lacp input routine
136  */
139 {
140  bond_main_t *bm = &bond_main;
141  lacp_main_t *lm = &lacp_main;
142  slave_if_t *sif;
143  uword nbytes;
144  lacp_error_t e;
145  marker_pdu_t *marker;
146  uword last_packet_signature;
147  bond_if_t *bif;
148 
149  sif =
151  if ((sif == 0) || (sif->mode != BOND_MODE_LACP))
152  {
153  return LACP_ERROR_DISABLED;
154  }
155 
156  /* Handle marker protocol */
157  marker = (marker_pdu_t *) (b0->data + b0->current_data);
158  if (marker->subtype == MARKER_SUBTYPE)
159  {
161  if (sif->last_marker_pkt)
162  _vec_len (sif->last_marker_pkt) = 0;
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))
168  {
170  return LACP_ERROR_TOO_SMALL;
171  }
172  e = handle_marker_protocol (vm, sif);
173  sif->marker_pdu_received++;
174  return e;
175  }
176 
177  /*
178  * typical clib idiom. Don't repeatedly allocate and free
179  * the per-neighbor rx buffer. Reset its apparent length to zero
180  * and reuse it.
181  */
182  if (sif->last_rx_pkt)
183  _vec_len (sif->last_rx_pkt) = 0;
184 
185  /*
186  * Make sure the per-neighbor rx buffer is big enough to hold
187  * the data we're about to copy
188  */
190 
191  /*
192  * Coalesce / copy the buffer chain into the per-neighbor
193  * rx buffer
194  */
195  nbytes = vlib_buffer_contents (vm, bi0, sif->last_rx_pkt);
196  ASSERT (nbytes <= vec_len (sif->last_rx_pkt));
197 
199  if (nbytes < sizeof (lacp_pdu_t))
200  {
201  sif->bad_pdu_received++;
202  return LACP_ERROR_TOO_SMALL;
203  }
204 
205  last_packet_signature =
206  hash_memory (sif->last_rx_pkt, vec_len (sif->last_rx_pkt), 0xd00b);
207 
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))
211  {
213  sif->ttl_in_seconds);
214  e = LACP_ERROR_CACHE_HIT;
215  }
216  else
217  {
218  /* Actually scan the packet */
219  e = lacp_packet_scan (vm, sif);
222  [sif->sw_if_index].actor_state,
223  sif->actor.state);
225  [sif->sw_if_index].partner_state,
226  sif->partner.state);
228  sif->last_packet_signature = last_packet_signature;
229  }
230  sif->pdu_received++;
231 
232  if (sif->last_rx_pkt)
233  _vec_len (sif->last_rx_pkt) = 0;
234 
235  return e;
236 }
237 
238 /*
239  * setup neighbor hash table
240  */
241 static clib_error_t *
243 {
244  return 0;
245 }
246 
247 /* *INDENT-OFF* */
249 {
250  .runs_after = VLIB_INITS("lacp_periodic_init"),
251 };
252 /* *INDENT-ON* */
253 
254 /*
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
258  */
259 u8 *
260 lacp_input_format_trace (u8 * s, va_list * args)
261 {
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;
267  int i, len;
268  u8 *p;
269  lacp_state_struct *state_entry;
270 
272  t->sw_if_index);
273  s = format (s, "Length: %d\n", t->len);
274  if (t->len >= sizeof (lacp_pdu_t))
275  {
276  switch (lacpdu->subtype)
277  {
278  case MARKER_SUBTYPE:
279  if (marker->version_number == MARKER_PROTOCOL_VERSION)
280  s = format (s, " Markerv1\n");
281  else
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);
294  break;
295 
296  case LACP_SUBTYPE:
297  if (lacpdu->version_number == LACP_ACTOR_LACP_VERSION)
298  s = format (s, " LACPv1\n");
299  else
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)
316  {
317  if (lacpdu->actor.port_info.state & (1 << state_entry->bit))
318  s = format (s, " %s (%d)\n", state_entry->str,
319  state_entry->bit);
320  state_entry++;
321  }
322 
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));
329  s =
330  format (s, " Key %u\n", ntohs (lacpdu->partner.port_info.key));
331  s =
332  format (s, " Port priority %u\n",
333  ntohs (lacpdu->partner.port_info.port_priority));
334  s =
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)
340  {
341  if (lacpdu->partner.port_info.state & (1 << state_entry->bit))
342  s = format (s, " %s (%d)\n", state_entry->str,
343  state_entry->bit);
344  state_entry++;
345  }
346  break;
347 
348  default:
349  break;
350  }
351  }
352 
353  if (t->len > sizeof (lacp_pdu_t))
354  len = sizeof (lacp_pdu_t);
355  else
356  len = t->len;
357  p = (u8 *) lacpdu;
358  for (i = 0; i < len; i++)
359  {
360  if ((i % 16) == 0)
361  {
362  if (i)
363  s = format (s, "\n");
364  s = format (s, " 0x%04x: ", i);
365  }
366  if ((i % 2) == 0)
367  s = format (s, " ");
368  s = format (s, "%02x", p[i]);
369  }
370 
371  return s;
372 }
373 
374 /*
375  * fd.io coding-style-patch-verification: ON
376  *
377  * Local Variables:
378  * eval: (c-set-style "gnu")
379  * End:
380  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:440
vlib_main_t * vlib_main
Definition: node.h:121
#define CLIB_UNUSED(x)
Definition: clib.h:82
lacp_stats_t ** stats
Definition: node.h:388
u64 marker_pdu_sent
Definition: node.h:346
static void send_ethernet_marker_response_pdu(slave_if_t *sif)
Definition: input.c:65
u8 last_packet_signature_valid
Definition: node.h:235
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
static int lacp_packet_scan(vlib_main_t *vm, slave_if_t *sif)
Definition: input.c:22
f64 last_lacpdu_recd_time
Definition: node.h:295
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
u8 * last_marker_pkt
Definition: node.h:242
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
u8 data[0]
Packet data.
Definition: buffer.h:181
int i
bond_main_t bond_main
Definition: node.c:25
lacp_state_struct lacp_state_array[]
Definition: node.c:21
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u64 pdu_received
Definition: node.h:331
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:366
u8 mode
Definition: node.h:328
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void marker_fill_pdu(marker_pdu_t *marker, slave_if_t *sif)
Definition: input.c:46
u8 * lacp_input_format_trace(u8 *s, va_list *args)
Definition: input.c:260
f32 ttl_in_seconds
Definition: node.h:221
marker_pdu_t marker
Definition: node.h:86
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:400
vl_api_interface_index_t sw_if_index
Definition: gre.api:59
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vnet_main_t * vnet_main
Definition: node.h:122
f64 last_marker_pdu_recd_time
Definition: node.h:301
u32 sw_if_index
Definition: node.h:184
u64 bad_pdu_received
Definition: node.h:334
lacp_port_info_t actor
Definition: node.h:255
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:185
u64 marker_pdu_received
Definition: node.h:340
static int handle_marker_protocol(vlib_main_t *vm, slave_if_t *sif)
Definition: input.c:117
unsigned int u32
Definition: types.h:88
u8 * last_rx_pkt
Definition: node.h:239
#define MARKER_SUBTYPE
Definition: protocol.h:132
f64 last_marker_pdu_sent_time
Definition: node.h:298
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:194
#define LACP_SUBTYPE
Definition: protocol.h:25
#define MARKER_PROTOCOL_VERSION
Definition: protocol.h:133
vlib_main_t * vm
Definition: in2out_ed.c:1810
static uword vlib_buffer_contents(vlib_main_t *vm, u32 buffer_index, u8 *contents)
Copy buffer contents to memory.
Definition: buffer_funcs.h:400
u8 len
Definition: ip_types.api:91
u32 marker_tx_id
Definition: node.h:321
u32 partner_state
Definition: node.h:357
u16 n_vectors
Definition: node.h:397
u32 sw_if_index
Definition: node.h:218
lacp_port_info_t partner
Definition: node.h:254
lacp_error_t
Definition: node.h:69
int rx_state
Definition: node.h:313
lacp_main_t lacp_main
Definition: lacp.c:26
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
#define ASSERT(truth)
lacp_pdu_t lacpdu
Definition: node.h:87
lacp_error_t lacp_input(vlib_main_t *vm, vlib_buffer_t *b0, u32 bi0)
Definition: input.c:138
static bond_if_t * bond_get_master_by_dev_instance(u32 dev_instance)
Definition: node.h:516
uword hash_memory(void *p, word n_bytes, uword state)
Definition: hash.c:275
u32 bif_dev_instance
Definition: node.h:323
vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES]
Definition: node.h:131
void marker_fill_request_pdu(marker_pdu_t *marker, slave_if_t *sif)
Definition: input.c:55
union lacp_input_trace_t::@56 pkt
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)
u32 sw_if_index
Definition: node.h:82
VLIB buffer representation.
Definition: buffer.h:102
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:244
static void lacp_start_current_while_timer(vlib_main_t *vm, slave_if_t *sif, u8 expiration)
Definition: rx_machine.h:77
#define LACP_STEADY_STATE
Definition: protocol.h:64
#define vnet_buffer(b)
Definition: buffer.h:408
static clib_error_t * lacp_init(vlib_main_t *vm)
Definition: input.c:242
u32 actor_state
Definition: node.h:358
static slave_if_t * bond_get_slave_by_sw_if_index(u32 sw_if_index)
Definition: node.h:524
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
uword last_packet_signature
Definition: node.h:236
u8 packet_template_index
Definition: node.h:230
#define VLIB_INITS(...)
Definition: init.h:344
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
u64 marker_bad_pdu_received
Definition: node.h:343
int lacp_machine_dispatch(lacp_machine_t *machine, vlib_main_t *vm, slave_if_t *sif, int event, int *state)
Definition: lacp.c:319
Definition: defs.h:46
void stat_segment_set_state_counter(u32 index, u64 value)
Definition: stat_segment.c:854
#define LACP_ACTOR_LACP_VERSION
Definition: protocol.h:26