aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/l2/l2_fib.h
blob: 202e2d9a33b92223a6529eb4111ae8ae05c7b878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * l2_fib.h : layer 2 forwarding table (aka mac table)
 *
 * Copyright (c) 2013 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef included_l2fib_h
#define included_l2fib_h

#include <vlib/vlib.h>
#include <vppinfra/bihash_8_8.h>

/*
 * The size of the hash table
 */
#define L2FIB_NUM_BUCKETS (256 * 1024)
#define L2FIB_MEMORY_SIZE (128<<20)

/* Ager scan interval is 1 minute for aging */
#define L2FIB_AGE_SCAN_INTERVAL		(60.0)

/* MAC event scan delay is 100 msec unless specified by MAC event client */
#define L2FIB_EVENT_SCAN_DELAY_DEFAULT	(0.1)

/* Max MACs in a event message is 100 unless specified by MAC event client */
#define L2FIB_EVENT_MAX_MACS_DEFAULT	(100)

/* MAC event learn limit is 1000 unless specified by MAC event client */
#define L2FIB_EVENT_LEARN_LIMIT_DEFAULT	(1000)

typedef struct
{

  /* hash table */
  BVT (clib_bihash) mac_table;

  /* number of buckets in the hash table */
  uword mac_table_n_buckets;

  /* hash table memory size */
  uword mac_table_memory_size;

  /* hash table initialized */
  u8 mac_table_initialized;

  /* last event or ager scan duration */
  f64 evt_scan_duration;
  f64 age_scan_duration;

  /* delay between event scans, default to 100 msec */
  f64 event_scan_delay;

  /* max macs in event message, default to 100 entries */
  u32 max_macs_in_event;

  /* convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} l2fib_main_t;

extern l2fib_main_t l2fib_main;

/*
 * The L2fib key is the mac address and bridge domain ID
 */
typedef struct
{
  union
  {
    struct
    {
      u16 bd_index;
      u8 mac[6];
    } fields;
    struct
    {
      u32 w0;
      u32 w1;
    } words;
    u64 raw;
  };
} l2fib_entry_key_t;

STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);

/**
 * A combined representation of the sequence number associated
 * with the interface and the BD.
 * The BD is in higher bits, the interface in the lower bits, but
 * the order is not important.
 *
 * It's convenient to represent this as an union of two u8s,
 * but then in the DP one is forced to do short writes, followed
 * by long reads, which is a sure thing for a stall
 */
typedef u16 l2fib_seq_num_t;

static_always_inline l2fib_seq_num_t
l2_fib_mk_seq_num (u8 bd_sn, u8 if_sn)
{
  return (((u16) bd_sn) << 8) | if_sn;
}

static_always_inline l2fib_seq_num_t
l2_fib_update_seq_num (l2fib_seq_num_t sn, u8 if_sn)
{
  sn &= 0xff00;
  sn |= if_sn;

  return (sn);
}

extern void l2_fib_extract_seq_num (l2fib_seq_num_t sn, u8 * bd_sn,
				    u8 * if_sn);
extern u8 *format_l2_fib_seq_num (u8 * s, va_list * a);

/**
 * Flags associated with an L2 Fib Entry
 *   - static mac, no MAC move
 *   - not subject to age
 *   - mac is for a bridged virtual interface
 *   - drop packets to/from this mac
 *   - MAC learned to be sent in L2 MAC event
 *   -MAC learned is a MAC move
 */
#define foreach_l2fib_entry_result_attr       \
  _(STATIC,  0, "static")                     \
  _(AGE_NOT, 1, "age-not")                    \
  _(BVI,     2, "bvi")                        \
  _(FILTER,  3, "filter")                     \
  _(LRN_EVT, 4, "learn-event")                \
  _(LRN_MOV, 5, "learn-move")

typedef enum l2fib_entry_result_flags_t_
{
  L2FIB_ENTRY_RESULT_FLAG_NONE = 0,
#define _(a,v,s) L2FIB_ENTRY_RESULT_FLAG_##a = (1 << v),
  foreach_l2fib_entry_result_attr
#undef _
} __attribute__ ((packed)) l2fib_entry_result_flags_t;

STATIC_ASSERT_SIZEOF (l2fib_entry_result_flags_t, 1);

extern u8 *format_l2fib_entry_result_flags (u8 * s, va_list * args);

/*
 * The l2fib entry results
 */
typedef struct l2fib_entry_result_t_
{
  union
  {
    struct
    {
      u32 sw_if_index;		/* output sw_if_index (L3 intf if bvi==1) */
      l2fib_entry_result_flags_t flags;

      u8 timestamp;		/* timestamp for aging */
      l2fib_seq_num_t sn;	/* bd/int seq num */
    } fields;
    u64 raw;
  };
} l2fib_entry_result_t;

STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);

#define _(a,v,s)                                                        \
  always_inline int                                                     \
  l2fib_entry_result_is_set_##a (const l2fib_entry_result_t *r) {       \
    return (r->fields.flags & L2FIB_ENTRY_RESULT_FLAG_##a);             \
  }
foreach_l2fib_entry_result_attr
#undef _
#define _(a,v,s)                                                        \
  always_inline void                                                    \
  l2fib_entry_result_set_##a (l2fib_entry_result_t *r) {       \
    r->fields.flags |= L2FIB_ENTRY_RESULT_FLAG_##a;             \
  }
  foreach_l2fib_entry_result_attr
#undef _
#define _(a,v,s)                                                        \
  always_inline void                                                    \
  l2fib_entry_result_clear_##a (l2fib_entry_result_t *r) {       \
    r->fields.flags &= ~L2FIB_ENTRY_RESULT_FLAG_##a;             \
  }
  foreach_l2fib_entry_result_attr
#undef _
  static inline void
l2fib_entry_result_set_bits (l2fib_entry_result_t * r,
			     l2fib_entry_result_flags_t bits)
{
  r->fields.flags |= bits;
}

static inline void
l2fib_entry_result_clear_bits (l2fib_entry_result_t * r,
			       l2fib_entry_result_flags_t bits)
{
  r->fields.flags &= ~bits;
}

/* L2 MAC event entry action enums (see mac_entry definition in l2.api) */
typedef enum
{
  MAC_EVENT_ACTION_ADD = 0,
  MAC_EVENT_ACTION_DELETE = 1,
  MAC_EVENT_ACTION_MOVE = 2,
} l2_mac_event_action_t;

/**
 * Compute the hash for the given key and return
 * the corresponding bucket index
 */
always_inline u32
l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
{
  u32 result;
  u32 temp_a;
  u32 temp_b;

  result = 0xa5a5a5a5;		/* some seed */
  temp_a = key->words.w0;
  temp_b = key->words.w1;
  hash_mix32 (temp_a, temp_b, result);

  return result % L2FIB_NUM_BUCKETS;
}

always_inline u64
l2fib_make_key (const u8 * mac_address, u16 bd_index)
{
  u64 temp;

  /*
   * The mac address in memory is A:B:C:D:E:F
   * The bd id in register is H:L
   */
#if CLIB_ARCH_IS_LITTLE_ENDIAN
  /*
   * Create the in-register key as F:E:D:C:B:A:H:L
   * In memory the key is L:H:A:B:C:D:E:F
   */
  temp = CLIB_MEM_OVERFLOW_LOAD ((u64 *) mac_address) << 16;
  temp = (temp & ~0xffff) | (u64) (bd_index);
#else
  /*
   * Create the in-register key as H:L:A:B:C:D:E:F
   * In memory the key is H:L:A:B:C:D:E:F
   */
  temp = CLIB_MEM_OVERFLOW_LOAD ((u64 *) mac_address) >> 16;
  temp = temp | (((u64) bd_index) << 48);
#endif

  return temp;
}



/**
 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
 * Cached_key and cached_result are used as a one-entry cache.
 * The function reads and updates them as needed.
 *
 * mac0 and bd_index0 are the keys. The entry is written to result0.
 * If the entry was not found, result0 is set to ~0.
 *
 * key0 return with the computed key, convenient if the entry needs,
 * to be updated afterward.
 */

static_always_inline void
l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
		l2fib_entry_key_t * cached_key,
		l2fib_entry_result_t * cached_result,
		u8 * mac0,
		u16 bd_index0,
		l2fib_entry_key_t * key0, l2fib_entry_result_t * result0)
{
  /* set up key */
  key0->raw = l2fib_make_key (mac0, bd_index0);

  if (key0->raw == cached_key->raw)
    {
      /* Hit in the one-entry cache */
      result0->raw = cached_result->raw;
    }
  else
    {
      /* Do a regular mac table lookup */
      BVT (clib_bihash_kv) kv;

      kv.key = key0->raw;
      kv.value = ~0ULL;
      BV (clib_bihash_search_inline) (mac_table, &kv);
      result0->raw = kv.value;

      /* Update one-entry cache */
      cached_key->raw = key0->raw;
      cached_result->raw = result0->raw;
    }
}


/**
 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
 * The lookups for the two packets are interleaved.
 *
 * Cached_key and cached_result are used as a one-entry cache.
 * The function reads and updates them as needed.
 *
 * mac0 and bd_index0 are the keys. The entry is written to result0.
 * If the entry was not found, result0 is set to ~0. The same
 * holds for mac1/bd_index1/result1.
 */
static_always_inline void
l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
		l2fib_entry_key_t * cached_key,
		l2fib_entry_result_t * cached_result,
		u8 * mac0,
		u8 * mac1,
		u16 bd_index0,
		u16 bd_index1,
		l2fib_entry_key_t * key0,
		l2fib_entry_key_t * key1,
		l2fib_entry_result_t * result0,
		l2fib_entry_result_t * result1)
{
  /* set up key */
  key0->raw = l2fib_make_key (mac0, bd_index0);
  key1->raw = l2fib_make_key (mac1, bd_index1);

  if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
    {
      /* Both hit in the one-entry cache */
      result0->raw = cached_result->raw;
      result1->raw = cached_result->raw;
    }
  else
    {
      BVT (clib_bihash_kv) kv0, kv1;

      /*
       * Do a regular mac table lookup
       * Interleave lookups for packet 0 and packet 1
       */
      kv0.key = key0->raw;
      kv1.key = key1->raw;
      kv0.value = ~0ULL;
      kv1.value = ~0ULL;

      BV (clib_bihash_search_inline) (mac_table, &kv0);
      BV (clib_bihash_search_inline) (mac_table, &kv1);

      result0->raw = kv0.value;
      result1->raw = kv1.value;

      /* Update one-entry cache */
      cached_key->raw = key1->raw;
      cached_result->raw = result1->raw;
    }
}

static_always_inline void
l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
		l2fib_entry_key_t * cached_key,
		l2fib_entry_result_t * cached_result,
		const u8 * mac0,
		const u8 * mac1,
		const u8 * mac2,
		const u8 * mac3,
		u16 bd_index0,
		u16 bd_index1,
		u16 bd_index2,
		u16 bd_index3,
		l2fib_entry_key_t * key0,
		l2fib_entry_key_t * key1,
		l2fib_entry_key_t * key2,
		l2fib_entry_key_t * key3,
		l2fib_entry_result_t * result0,
		l2fib_entry_result_t * result1,
		l2fib_entry_result_t * result2,
		l2fib_entry_result_t * result3)
{
  /* set up key */
  key0->raw = l2fib_make_key (mac0, bd_index0);
  key1->raw = l2fib_make_key (mac1, bd_index1);
  key2->raw = l2fib_make_key (mac2, bd_index2);
  key3->raw = l2fib_make_key (mac3, bd_index3);

  if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
      (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
    {
      /* Both hit in the one-entry cache */
      result0->raw = cached_result->raw;
      result1->raw = cached_result->raw;
      result2->raw = cached_result->raw;
      result3->raw = cached_result->raw;
    }
  else
    {
      BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;

      /*
       * Do a regular mac table lookup
       * Interleave lookups for packet 0 and packet 1
       */
      kv0.key = key0->raw;
      kv1.key = key1->raw;
      kv2.key = key2->raw;
      kv3.key = key3->raw;
      kv0.value = ~0ULL;
      kv1.value = ~0ULL;
      kv2.value = ~0ULL;
      kv3.value = ~0ULL;

      BV (clib_bihash_search_inline) (mac_table, &kv0);
      BV (clib_bihash_search_inline) (mac_table, &kv1);
      BV (clib_bihash_search_inline) (mac_table, &kv2);
      BV (clib_bihash_search_inline) (mac_table, &kv3);

      result0->raw = kv0.value;
      result1->raw = kv1.value;
      result2->raw = kv2.value;
      result3->raw = kv3.value;

      /* Update one-entry cache */
      cached_key->raw = key1->raw;
      cached_result->raw = result1->raw;
    }
}

void l2fib_clear_table (void);

void l2fib_table_init (void);

void
l2fib_add_entry (const u8 * mac,
		 u32 bd_index,
		 u32 sw_if_index, l2fib_entry_result_flags_t flags);

static inline void
l2fib_add_filter_entry (const u8 * mac, u32 bd_index)
{
  l2fib_add_entry (mac, bd_index, ~0,
		   (L2FIB_ENTRY_RESULT_FLAG_FILTER |
		    L2FIB_ENTRY_RESULT_FLAG_STATIC));
}

u32 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index);

void l2fib_start_ager_scan (vlib_main_t * vm);

void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);

void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);

void l2fib_flush_all_mac (vlib_main_t * vm);

void
l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
		  l2fib_entry_result_t ** l2fe_res);

u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);

BVT (clib_bihash) * get_mac_table (void);

#endif

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
/span> vnet_dev_hw_addr_t *addr; int found; if (req->validated) return VNET_DEV_OK; switch (req->type) { case VNET_DEV_PORT_CFG_MAX_RX_FRAME_SIZE: if (req->max_rx_frame_size > port->attr.max_supported_rx_frame_size) return VNET_DEV_ERR_INVALID_VALUE; if (req->max_rx_frame_size == port->max_rx_frame_size) return VNET_DEV_ERR_NO_CHANGE; break; case VNET_DEV_PORT_CFG_PROMISC_MODE: if (req->promisc == port->promisc) return VNET_DEV_ERR_NO_CHANGE; break; case VNET_DEV_PORT_CFG_CHANGE_PRIMARY_HW_ADDR: if (clib_memcmp (&req->addr, &port->primary_hw_addr, sizeof (vnet_dev_hw_addr_t)) == 0) return VNET_DEV_ERR_NO_CHANGE; break; case VNET_DEV_PORT_CFG_ADD_SECONDARY_HW_ADDR: pool_foreach (addr, port->secondary_hw_addr) if (clib_memcmp (addr, &req->addr, sizeof (*addr)) == 0) return VNET_DEV_ERR_ALREADY_EXISTS; break; case VNET_DEV_PORT_CFG_REMOVE_SECONDARY_HW_ADDR: found = 0; pool_foreach (addr, port->secondary_hw_addr) if (clib_memcmp (addr, &req->addr, sizeof (*addr)) == 0) found = 1; if (!found) return VNET_DEV_ERR_NO_SUCH_ENTRY; break; default: break; } if (port->port_ops.config_change_validate) { rv = port->port_ops.config_change_validate (vm, port, req); if (rv != VNET_DEV_OK) return rv; } else return VNET_DEV_ERR_NOT_SUPPORTED; req->validated = 1; return VNET_DEV_OK; } vnet_dev_rv_t vnet_dev_port_cfg_change (vlib_main_t *vm, vnet_dev_port_t *port, vnet_dev_port_cfg_change_req_t *req) { vnet_dev_rv_t rv = VNET_DEV_OK; vnet_dev_hw_addr_t *a; vnet_dev_rx_queue_t *rxq = 0; u8 enable = 0; vnet_dev_port_validate (vm, port); if (req->type == VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE || req->type == VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE) { if (req->all_queues == 0) { rxq = vnet_dev_port_get_rx_queue_by_id (port, req->queue_id); if (rxq == 0) return VNET_DEV_ERR_BUG; } } if ((rv = vnet_dev_port_cfg_change_req_validate (vm, port, req))) return rv; if (port->port_ops.config_change) rv = port->port_ops.config_change (vm, port, req); else return VNET_DEV_ERR_NOT_SUPPORTED; if (rv != VNET_DEV_OK) return rv; switch (req->type) { case VNET_DEV_PORT_CFG_MAX_RX_FRAME_SIZE: port->max_rx_frame_size = req->max_rx_frame_size; break; case VNET_DEV_PORT_CFG_PROMISC_MODE: port->promisc = req->promisc; break; case VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE: enable = 1; case VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE: if (req->all_queues) { clib_bitmap_t *bmp = 0; vnet_dev_rt_op_t *ops = 0; u32 i; foreach_vnet_dev_port_rx_queue (q, port) { q->interrupt_mode = enable; bmp = clib_bitmap_set (bmp, q->rx_thread_index, 1); } clib_bitmap_foreach (i, bmp) { vnet_dev_rt_op_t op = { .port = port, .thread_index = i }; vec_add1 (ops, op); } vnet_dev_rt_exec_ops (vm, port->dev, ops, vec_len (ops)); clib_bitmap_free (bmp); vec_free (ops); } else { rxq->interrupt_mode = enable; vnet_dev_rt_exec_ops (vm, port->dev, &(vnet_dev_rt_op_t){ .port = port, .thread_index = rxq->rx_thread_index, }, 1); } break; case VNET_DEV_PORT_CFG_CHANGE_PRIMARY_HW_ADDR: clib_memcpy (&port->primary_hw_addr, &req->addr, sizeof (vnet_dev_hw_addr_t)); break; case VNET_DEV_PORT_CFG_ADD_SECONDARY_HW_ADDR: pool_get (port->secondary_hw_addr, a); clib_memcpy (a, &req->addr, sizeof (vnet_dev_hw_addr_t)); break; case VNET_DEV_PORT_CFG_REMOVE_SECONDARY_HW_ADDR: pool_foreach (a, port->secondary_hw_addr) if (clib_memcmp (a, &req->addr, sizeof (vnet_dev_hw_addr_t)) == 0) { pool_put (port->secondary_hw_addr, a); break; } break; default: break; } return VNET_DEV_OK; } void vnet_dev_port_state_change (vlib_main_t *vm, vnet_dev_port_t *port, vnet_dev_port_state_changes_t changes) { vnet_main_t *vnm = vnet_get_main (); vnet_dev_port_validate (vm, port); if (changes.change.link_speed) { port->speed = changes.link_speed; if (port->interface_created) vnet_hw_interface_set_link_speed (vnm, port->intf.hw_if_index, changes.link_speed); log_debug (port->dev, "port speed changed to %u", changes.link_speed); } if (changes.change.link_state) { port->link_up = changes.link_state; if (port->interface_created) vnet_hw_interface_set_flags ( vnm, port->intf.hw_if_index, changes.link_state ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0); log_debug (port->dev, "port link state changed to %s", changes.link_state ? "up" : "down"); } } void vnet_dev_port_add_counters (vlib_main_t *vm, vnet_dev_port_t *port, vnet_dev_counter_t *counters, u16 n_counters) { vnet_dev_port_validate (vm, port); port->counter_main = vnet_dev_counters_alloc (vm, counters, n_counters, "%s port %u counters", port->dev->device_id, port->port_id); } void vnet_dev_port_free_counters (vlib_main_t *vm, vnet_dev_port_t *port) { vnet_dev_port_validate (vm, port); if (port->counter_main) vnet_dev_counters_free (vm, port->counter_main); } vnet_dev_rv_t vnet_dev_port_if_create (vlib_main_t *vm, vnet_dev_port_t *port) { vnet_main_t *vnm = vnet_get_main (); u16 n_threads = vlib_get_n_threads (); vnet_dev_main_t *dm = &vnet_dev_main; vnet_dev_t *dev = port->dev; vnet_dev_port_t **pp; vnet_dev_rv_t rv; u16 ti = 0; if (port->intf.name[0] == 0) { u8 *s; s = format (0, "%s%u/%u", dm->drivers[port->dev->driver_index].registration->name, port->dev->index, port->index); u32 n = vec_len (s); if (n >= sizeof (port->intf.name)) { vec_free (s); return VNET_DEV_ERR_BUG; } clib_memcpy (port->intf.name, s, n); port->intf.name[n] = 0; vec_free (s); } log_debug ( dev, "allocating %u rx queues with size %u and %u tx queues with size %u", port->intf.num_rx_queues, port->intf.rxq_sz, port->intf.num_tx_queues, port->intf.txq_sz); for (int i = 0; i < port->intf.num_rx_queues; i++) if ((rv = vnet_dev_rx_queue_alloc (vm, port, port->intf.rxq_sz)) != VNET_DEV_OK) goto error; for (u32 i = 0; i < port->intf.num_tx_queues; i++) if ((rv = vnet_dev_tx_queue_alloc (vm, port, port->intf.txq_sz)) != VNET_DEV_OK) goto error; foreach_vnet_dev_port_tx_queue (q, port) { q->assigned_threads = clib_bitmap_set (q->assigned_threads, ti, 1); log_debug (dev, "port %u tx queue %u assigned to thread %u", port->port_id, q->queue_id, ti); if (++ti >= n_threads) break; } /* pool of port pointers helps us to assign unique dev_instance */ pool_get (dm->ports_by_dev_instance, pp); port->intf.dev_instance = pp - dm->ports_by_dev_instance; pp[0] = port; if (port->attr.type == VNET_DEV_PORT_TYPE_ETHERNET) { vnet_device_class_t *dev_class; vnet_dev_driver_t *driver; vnet_sw_interface_t *sw; vnet_hw_interface_t *hw; u32 rx_node_index; driver = pool_elt_at_index (dm->drivers, dev->driver_index); /* hack to provide per-port tx node function */ dev_class = vnet_get_device_class (vnm, driver->dev_class_index); dev_class->tx_fn_registrations = port->tx_node.registrations; dev_class->format_tx_trace = port->tx_node.format_trace; dev_class->tx_function_error_counters = port->tx_node.error_counters; dev_class->tx_function_n_errors = port->tx_node.n_error_counters; /* create new interface including tx and output nodes */ port->intf.hw_if_index = vnet_eth_register_interface ( vnm, &(vnet_eth_interface_registration_t){ .address = port->primary_hw_addr.eth_mac, .max_frame_size = port->max_rx_frame_size, .dev_class_index = driver->dev_class_index, .dev_instance = port->intf.dev_instance, .cb.set_max_frame_size = vnet_dev_port_set_max_frame_size, .cb.flag_change = vnet_dev_port_eth_flag_change, }); sw = vnet_get_hw_sw_interface (vnm, port->intf.hw_if_index); hw = vnet_get_hw_interface (vnm, port->intf.hw_if_index); port->intf.sw_if_index = sw->sw_if_index; vnet_hw_interface_set_flags ( vnm, port->intf.hw_if_index, port->link_up ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0); if (port->speed) vnet_hw_interface_set_link_speed (vnm, port->intf.hw_if_index, port->speed); port->intf.tx_node_index = hw->tx_node_index; /* create / reuse rx node */ if (vec_len (dm->free_rx_node_indices)) { vlib_node_t *n; rx_node_index = vec_pop (dm->free_rx_node_indices); vlib_node_rename (vm, rx_node_index, "%s-rx", port->intf.name); n = vlib_get_node (vm, rx_node_index); n->function = vlib_node_get_preferred_node_fn_variant ( vm, port->rx_node.registrations); n->format_trace = port->rx_node.format_trace; vlib_register_errors (vm, rx_node_index, port->rx_node.n_error_counters, 0, port->rx_node.error_counters); } else { dev_class->format_tx_trace = port->tx_node.format_trace; dev_class->tx_function_error_counters = port->tx_node.error_counters; dev_class->tx_function_n_errors = port->tx_node.n_error_counters; vlib_node_registration_t rx_node_reg = { .sibling_of = "port-rx-eth", .type = VLIB_NODE_TYPE_INPUT, .state = VLIB_NODE_STATE_DISABLED, .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, .node_fn_registrations = port->rx_node.registrations, .format_trace = port->rx_node.format_trace, .error_counters = port->rx_node.error_counters, .n_errors = port->rx_node.n_error_counters, }; rx_node_index = vlib_register_node (vm, &rx_node_reg, "%s-rx", port->intf.name); } port->rx_node_assigned = 1; port->intf.rx_node_index = rx_node_index; port->intf.rx_next_index = vnet_dev_default_next_index_by_port_type[port->attr.type]; vlib_worker_thread_node_runtime_update (); log_debug (dev, "ethernet interface created, hw_if_index %u sw_if_index %u " "rx_node_index %u tx_node_index %u", port->intf.hw_if_index, port->intf.sw_if_index, port->intf.rx_node_index, port->intf.tx_node_index); } port->interface_created = 1; foreach_vnet_dev_port_rx_queue (q, port) { vnet_buffer (&q->buffer_template)->sw_if_index[VLIB_RX] = port->intf.sw_if_index; /* poison to catch node not calling runtime update function */ q->next_index = ~0; q->interrupt_mode = port->intf.default_is_intr_mode; vnet_dev_rx_queue_rt_request ( vm, q, (vnet_dev_rx_queue_rt_req_t){ .update_next_index = 1 }); } vnet_dev_port_update_tx_node_runtime (vm, port); if (port->port_ops.init) rv = port->port_ops.init (vm, port); error: if (rv != VNET_DEV_OK) vnet_dev_port_if_remove (vm, port); return rv; } vnet_dev_rv_t vnet_dev_port_if_remove (vlib_main_t *vm, vnet_dev_port_t *port) { vnet_dev_main_t *dm = &vnet_dev_main; vnet_main_t *vnm = vnet_get_main (); vnet_dev_port_validate (vm, port); if (port->started) vnet_dev_port_stop (vm, port); if (port->rx_node_assigned) { vlib_node_rename (vm, port->intf.rx_node_index, "deleted-%u", port->intf.rx_node_index); vec_add1 (dm->free_rx_node_indices, port->intf.rx_node_index); port->rx_node_assigned = 0; } if (port->interface_created) { vlib_worker_thread_barrier_sync (vm); vnet_delete_hw_interface (vnm, port->intf.hw_if_index); vlib_worker_thread_barrier_release (vm); pool_put_index (dm->ports_by_dev_instance, port->intf.dev_instance); port->interface_created = 0; } port->intf = (typeof (port->intf)){}; if (port->port_ops.deinit) port->port_ops.deinit (vm, port); foreach_vnet_dev_port_tx_queue (q, port) vnet_dev_tx_queue_free (vm, q); foreach_vnet_dev_port_rx_queue (q, port) vnet_dev_rx_queue_free (vm, q); vnet_dev_port_free_counters (vm, port); foreach_vnet_dev_port_args (v, port) vnet_dev_arg_clear_value (v); return VNET_DEV_OK; } void vnet_dev_port_clear_counters (vlib_main_t *vm, vnet_dev_port_t *port) { if (port->counter_main) vnet_dev_counters_clear (vm, port->counter_main); foreach_vnet_dev_port_rx_queue (q, port) if (q->counter_main) vnet_dev_counters_clear (vm, q->counter_main); foreach_vnet_dev_port_tx_queue (q, port) if (q->counter_main) vnet_dev_counters_clear (vm, q->counter_main); log_notice (port->dev, "counters cleared on port %u", port->port_id); }