aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/interface/tx_queue.c
blob: 3041a58414d282483fdaf56c19c4db5fdc928107 (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2021 Cisco Systems, Inc.
 */

#include <vnet/vnet.h>
#include <vnet/devices/devices.h>
#include <vnet/interface/tx_queue_funcs.h>
#include <vlib/unix/unix.h>

VLIB_REGISTER_LOG_CLASS (if_txq_log, static) = {
  .class_name = "interface",
  .subclass_name = "tx-queue",
};

#define log_debug(fmt, ...) vlib_log_debug (if_txq_log.class, fmt, __VA_ARGS__)
#define log_err(fmt, ...)   vlib_log_err (if_txq_log.class, fmt, __VA_ARGS__)

static u64
tx_queue_key (u32 hw_if_index, u32 queue_id)
{
  return ((u64) hw_if_index << 32) | queue_id;
}

u32
vnet_hw_if_get_tx_queue_index_by_id (vnet_main_t *vnm, u32 hw_if_index,
				     u32 queue_id)
{
  vnet_interface_main_t *im = &vnm->interface_main;
  u64 key = tx_queue_key (hw_if_index, queue_id);
  uword *p = hash_get_mem (im->txq_index_by_hw_if_index_and_queue_id, &key);
  return p ? p[0] : ~0;
}

u32
vnet_hw_if_register_tx_queue (vnet_main_t *vnm, u32 hw_if_index, u32 queue_id)
{
  vnet_interface_main_t *im = &vnm->interface_main;
  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
  vnet_hw_if_tx_queue_t *txq;
  u64 key = tx_queue_key (hw_if_index, queue_id);
  u32 queue_index;

  if (hash_get_mem (im->txq_index_by_hw_if_index_and_queue_id, &key))
    clib_panic ("Trying to register already registered queue id (%u) in the "
		"interface %v\n",
		queue_id, hi->name);

  pool_get_zero (im->hw_if_tx_queues, txq);
  queue_index = txq - im->hw_if_tx_queues;
  vec_add1 (hi->tx_queue_indices, queue_index);
  hash_set_mem_alloc (&im->txq_index_by_hw_if_index_and_queue_id, &key,
		      queue_index);
  txq->hw_if_index = hw_if_index;
  txq->queue_id = queue_id;

  log_debug ("register: interface %v queue-id %u", hi->name, queue_id);

  return queue_index;
}

void
vnet_hw_if_unregister_tx_queue (vnet_main_t *vnm, u32 queue_index)
{
  vnet_interface_main_t *im = &vnm->interface_main;
  vnet_hw_if_tx_queue_t *txq;
  txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, txq->hw_if_index);
  u64 key;

  key = tx_queue_key (txq->hw_if_index, txq->queue_id);
  hash_unset_mem_free (&im->txq_index_by_hw_if_index_and_queue_id, &key);

  for (int i = 0; i < vec_len (hi->tx_queue_indices); i++)
    if (hi->tx_queue_indices[i] == queue_index)
      {
	vec_del1 (hi->tx_queue_indices, i);
	break;
      }

  log_debug ("unregister: interface %v queue-id %u", hi->name, txq->queue_id);
  clib_bitmap_free (txq->threads);
  pool_put_index (im->hw_if_tx_queues, queue_index);
}

void
vnet_hw_if_unregister_all_tx_queues (vnet_main_t *vnm, u32 hw_if_index)
{
  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
  vnet_interface_main_t *im = &vnm->interface_main;
  vnet_hw_if_tx_queue_t *txq;
  u64 key;

  log_debug ("unregister_all: interface %v", hi->name);

  for (int i = 0; i < vec_len (hi->tx_queue_indices); i++)
    {
      txq = vnet_hw_if_get_tx_queue (vnm, hi->tx_queue_indices[i]);
      key = tx_queue_key (txq->hw_if_index, txq->queue_id);
      hash_unset_mem_free (&im->txq_index_by_hw_if_index_and_queue_id, &key);

      clib_bitmap_free (txq->threads);
      pool_put_index (im->hw_if_tx_queues, hi->tx_queue_indices[i]);
    }

  vec_free (hi->tx_queue_indices);
}

void
vnet_hw_if_tx_queue_assign_thread (vnet_main_t *vnm, u32 queue_index,
				   u32 thread_index)
{
  vnet_hw_if_tx_queue_t *txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, txq->hw_if_index);
  txq->threads = clib_bitmap_set (txq->threads, thread_index, 1);
  log_debug ("assign_thread: interface %v queue-id %u thread %u", hi->name,
	     txq->queue_id, thread_index);
}

void
vnet_hw_if_tx_queue_unassign_thread (vnet_main_t *vnm, u32 queue_index,
				     u32 thread_index)
{
  vnet_hw_if_tx_queue_t *txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, txq->hw_if_index);
  txq->threads = clib_bitmap_set (txq->threads, thread_index, 0);
  log_debug ("unassign_thread: interface %v queue-id %u thread %u", hi->name,
	     txq->queue_id, thread_index);
}