FD.io VPP  v16.09
Vector Packet Processing
netmap.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #include <stdint.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
24 
25 #include <vlib/vlib.h>
26 #include <vlib/unix/unix.h>
27 #include <vnet/ethernet/ethernet.h>
29 
30 static u32
32  u32 flags)
33 {
34  /* nothing for now */
35  return 0;
36 }
37 
38 static clib_error_t *
40 {
41  vlib_main_t *vm = vlib_get_main ();
43  u32 idx = uf->private_data;
44 
47 
48  /* Schedule the rx node */
50 
51  return 0;
52 }
53 
54 static void
56 {
57  if (nif->unix_file_index != ~0)
58  {
60  nif->unix_file_index = ~0;
61  }
62 
63  if (nif->fd > -1)
64  close (nif->fd);
65 
66  if (nif->mem_region)
67  {
68  netmap_mem_region_t *reg = &nm->mem_regions[nif->mem_region];
69  if (--reg->refcnt == 0)
70  {
71  munmap (reg->mem, reg->region_size);
72  reg->region_size = 0;
73  }
74  }
75 
76 
78  &nif->if_index);
79  vec_free (nif->host_if_name);
80  vec_free (nif->req);
81 
82  memset (nif, 0, sizeof (*nif));
83  pool_put (nm->interfaces, nif);
84 }
85 
86 int
88 {
89  /* if worker threads are enabled, switch to polling mode */
91  {
92  vlib_node_set_state (this_vlib_main,
93  netmap_input_node.index,
94  VLIB_NODE_STATE_POLLING);
95  }));
96 
97  return 0;
98 }
99 
100 int
102 {
104  {
105  vlib_node_set_state (this_vlib_main,
106  netmap_input_node.index,
107  VLIB_NODE_STATE_INTERRUPT);
108  }));
109 
110  return 0;
111 }
112 
113 int
114 netmap_create_if (vlib_main_t * vm, u8 * if_name, u8 * hw_addr_set,
115  u8 is_pipe, u8 is_master, u32 * sw_if_index)
116 {
117  netmap_main_t *nm = &netmap_main;
118  int ret = 0;
119  netmap_if_t *nif = 0;
120  u8 hw_addr[6];
121  clib_error_t *error = 0;
123  vnet_main_t *vnm = vnet_get_main ();
124  uword *p;
125  struct nmreq *req = 0;
126  netmap_mem_region_t *reg;
128  int fd;
129 
130  p = mhash_get (&nm->if_index_by_host_if_name, if_name);
131  if (p)
132  return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
133 
134  fd = open ("/dev/netmap", O_RDWR);
135  if (fd < 0)
136  return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
137 
138  pool_get (nm->interfaces, nif);
139  nif->if_index = nif - nm->interfaces;
140  nif->fd = fd;
141  nif->unix_file_index = ~0;
142 
143  vec_validate (req, 0);
144  nif->req = req;
145  req->nr_version = NETMAP_API;
146  req->nr_flags = NR_REG_ALL_NIC;
147 
148  if (is_pipe)
149  req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
150  else
151  req->nr_flags = NR_REG_ALL_NIC;
152 
154  snprintf (req->nr_name, IFNAMSIZ, "%s", if_name);
155  req->nr_name[IFNAMSIZ - 1] = 0;
156 
157  if (ioctl (nif->fd, NIOCREGIF, req))
158  {
159  ret = VNET_API_ERROR_NOT_CONNECTED;
160  goto error;
161  }
162 
163  nif->mem_region = req->nr_arg2;
164  vec_validate (nm->mem_regions, nif->mem_region);
165  reg = &nm->mem_regions[nif->mem_region];
166  if (reg->region_size == 0)
167  {
168  reg->mem = mmap (NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
169  MAP_SHARED, fd, 0);
170  clib_warning ("mem %p", reg->mem);
171  if (reg->mem == MAP_FAILED)
172  {
173  ret = VNET_API_ERROR_NOT_CONNECTED;
174  goto error;
175  }
176  reg->region_size = req->nr_memsize;
177  }
178  reg->refcnt++;
179 
180  nif->nifp = NETMAP_IF (reg->mem, req->nr_offset);
181  nif->first_rx_ring = 0;
182  nif->last_rx_ring = 0;
183  nif->first_tx_ring = 0;
184  nif->last_tx_ring = 0;
185  nif->host_if_name = if_name;
186  nif->per_interface_next_index = ~0;
187 
188  if (tm->n_vlib_mains > 1)
189  {
192  memset ((void *) nif->lockp, 0, CLIB_CACHE_LINE_BYTES);
193  }
194 
195  {
196  unix_file_t template = { 0 };
198  template.file_descriptor = nif->fd;
199  template.private_data = nif->if_index;
200  nif->unix_file_index = unix_file_add (&unix_main, &template);
201  }
202 
203  /*use configured or generate random MAC address */
204  if (hw_addr_set)
205  memcpy (hw_addr, hw_addr_set, 6);
206  else
207  {
208  f64 now = vlib_time_now (vm);
209  u32 rnd;
210  rnd = (u32) (now * 1e6);
211  rnd = random_u32 (&rnd);
212 
213  memcpy (hw_addr + 2, &rnd, sizeof (rnd));
214  hw_addr[0] = 2;
215  hw_addr[1] = 0xfe;
216  }
217 
219  nif->if_index, hw_addr,
220  &nif->hw_if_index,
222 
223  if (error)
224  {
225  clib_error_report (error);
226  ret = VNET_API_ERROR_SYSCALL_ERROR_1;
227  goto error;
228  }
229 
230  sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
231  nif->sw_if_index = sw->sw_if_index;
232 
233  mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
234 
235  if (sw_if_index)
236  *sw_if_index = nif->sw_if_index;
237 
238  if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 1)
240 
241  return 0;
242 
243 error:
244  close_netmap_if (nm, nif);
245  return ret;
246 }
247 
248 int
249 netmap_delete_if (vlib_main_t * vm, u8 * host_if_name)
250 {
251  vnet_main_t *vnm = vnet_get_main ();
252  netmap_main_t *nm = &netmap_main;
253  netmap_if_t *nif;
254  uword *p;
256 
257  p = mhash_get (&nm->if_index_by_host_if_name, host_if_name);
258  if (p == NULL)
259  {
260  clib_warning ("Host interface %s does not exist", host_if_name);
261  return VNET_API_ERROR_SYSCALL_ERROR_1;
262  }
263  nif = pool_elt_at_index (nm->interfaces, p[0]);
264 
265  /* bring down the interface */
267 
269 
270  close_netmap_if (nm, nif);
271 
272  if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 0)
274 
275  return 0;
276 }
277 
278 static clib_error_t *
280 {
281  netmap_main_t *nm = &netmap_main;
284  uword *p;
285 
286  memset (nm, 0, sizeof (netmap_main_t));
287 
288  nm->input_cpu_first_index = 0;
289  nm->input_cpu_count = 1;
290 
291  /* find out which cpus will be used for input */
292  p = hash_get_mem (tm->thread_registrations_by_name, "workers");
293  tr = p ? (vlib_thread_registration_t *) p[0] : 0;
294 
295  if (tr && tr->count > 0)
296  {
298  nm->input_cpu_count = tr->count;
299  }
300 
302 
305 
306  return 0;
307 }
308 
310 
311 /*
312  * fd.io coding-style-patch-verification: ON
313  *
314  * Local Variables:
315  * eval: (c-set-style "gnu")
316  * End:
317  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
unix_file_t * file_pool
Definition: unix.h:89
vmrglw vmrglh hi
uint32_t nr_offset
Definition: net_netmap.h:477
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:513
unix_file_function_t * read_function
Definition: unix.h:62
netmap_if_t * interfaces
Definition: netmap.h:78
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:230
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
int netmap_worker_thread_enable()
Definition: netmap.c:87
volatile u32 * lockp
Definition: netmap.h:46
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:179
#define NULL
Definition: clib.h:55
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:353
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
uint32_t nr_memsize
Definition: net_netmap.h:478
static void mhash_init_vec_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:84
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
#define clib_error_report(e)
Definition: error.h:125
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:407
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:136
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 sw_if_index
Definition: netmap.h:50
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define NIOCREGIF
Definition: net_netmap.h:595
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
u32 per_interface_next_index
Definition: netmap.h:53
char nr_name[IFNAMSIZ]
Definition: net_netmap.h:475
u32 input_cpu_count
Definition: netmap.h:96
#define clib_warning(format, args...)
Definition: error.h:59
struct netmap_if * nifp
Definition: netmap.h:60
#define NETMAP_API
Definition: net_netmap.h:42
struct nmreq * req
Definition: netmap.h:57
static void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:146
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
uword private_data
Definition: unix.h:59
netmap_mem_region_t * mem_regions
Definition: netmap.h:90
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:271
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
uint32_t nr_version
Definition: net_netmap.h:476
static u32 netmap_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
Definition: netmap.c:31
#define foreach_vlib_main(body)
Definition: threads.h:215
u16 first_rx_ring
Definition: netmap.h:63
mhash_t if_index_by_host_if_name
Definition: netmap.h:87
int fd
Definition: netmap.h:59
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
u16 first_tx_ring
Definition: netmap.h:61
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define NR_ACCEPT_VNET_HDR
Definition: net_netmap.h:543
u32 hw_if_index
Definition: netmap.h:49
int netmap_create_if(vlib_main_t *vm, u8 *if_name, u8 *hw_addr_set, u8 is_pipe, u8 is_master, u32 *sw_if_index)
Definition: netmap.c:114
static uword * mhash_get(mhash_t *h, void *key)
Definition: mhash.h:110
u16 last_rx_ring
Definition: netmap.h:64
static void close_netmap_if(netmap_main_t *nm, netmap_if_t *nif)
Definition: netmap.c:55
u16 last_tx_ring
Definition: netmap.h:62
uword if_index
Definition: netmap.h:48
netmap_main_t netmap_main
Definition: netmap.h:99
vlib_node_registration_t netmap_input_node
(constructor) VLIB_REGISTER_NODE (netmap_input_node)
Definition: node.c:293
unsigned int u32
Definition: types.h:88
u16 mem_region
Definition: netmap.h:58
u8 * host_if_name
Definition: netmap.h:47
int netmap_worker_thread_disable()
Definition: netmap.c:101
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:181
static clib_error_t * netmap_fd_read_ready(unix_file_t *uf)
Definition: netmap.c:39
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:144
uword * thread_registrations_by_name
Definition: threads.h:268
unix_main_t unix_main
Definition: main.c:57
u64 uword
Definition: types.h:112
u32 unix_file_index
Definition: netmap.h:51
u32 input_cpu_first_index
Definition: netmap.h:93
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
vnet_device_class_t netmap_device_class
Definition: unix.h:49
uint16_t nr_arg2
Definition: net_netmap.h:511
uword * pending_input_bitmap
Definition: netmap.h:81
#define hash_get_mem(h, key)
Definition: hash.h:268
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:114
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u32 ** rx_buffers
Definition: netmap.h:84
u32 flags
Definition: vhost-user.h:76
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
static clib_error_t * netmap_init(vlib_main_t *vm)
Definition: netmap.c:279
uint32_t nr_flags
Definition: net_netmap.h:513
int netmap_delete_if(vlib_main_t *vm, u8 *host_if_name)
Definition: netmap.c:249
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109