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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
23 #include <vnet/devices/netmap/net_netmap.h>
25 #include <vlib/vlib.h>
26 #include <vlib/unix/unix.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/devices/netmap/netmap.h>
31 netmap_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
39 netmap_fd_read_ready (unix_file_t * uf)
41 vlib_main_t *vm = vlib_get_main ();
42 netmap_main_t *nm = &netmap_main;
43 u32 idx = uf->private_data;
45 nm->pending_input_bitmap =
46 clib_bitmap_set (nm->pending_input_bitmap, idx, 1);
48 /* Schedule the rx node */
49 vlib_node_set_interrupt_pending (vm, netmap_input_node.index);
55 close_netmap_if (netmap_main_t * nm, netmap_if_t * nif)
57 if (nif->unix_file_index != ~0)
59 unix_file_del (&unix_main, unix_main.file_pool + nif->unix_file_index);
60 nif->unix_file_index = ~0;
68 netmap_mem_region_t *reg = &nm->mem_regions[nif->mem_region];
69 if (--reg->refcnt == 0)
71 munmap (reg->mem, reg->region_size);
77 mhash_unset (&nm->if_index_by_host_if_name, nif->host_if_name,
79 vec_free (nif->host_if_name);
82 memset (nif, 0, sizeof (*nif));
83 pool_put (nm->interfaces, nif);
87 netmap_worker_thread_enable ()
89 /* if worker threads are enabled, switch to polling mode */
92 vlib_node_set_state (this_vlib_main,
93 netmap_input_node.index,
94 VLIB_NODE_STATE_POLLING);
101 netmap_worker_thread_disable ()
105 vlib_node_set_state (this_vlib_main,
106 netmap_input_node.index,
107 VLIB_NODE_STATE_INTERRUPT);
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)
117 netmap_main_t *nm = &netmap_main;
119 netmap_if_t *nif = 0;
121 clib_error_t *error = 0;
122 vnet_sw_interface_t *sw;
123 vnet_main_t *vnm = vnet_get_main ();
125 struct nmreq *req = 0;
126 netmap_mem_region_t *reg;
127 vlib_thread_main_t *tm = vlib_get_thread_main ();
130 p = mhash_get (&nm->if_index_by_host_if_name, if_name);
132 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
134 fd = open ("/dev/netmap", O_RDWR);
136 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
138 pool_get (nm->interfaces, nif);
139 nif->if_index = nif - nm->interfaces;
141 nif->unix_file_index = ~0;
143 vec_validate (req, 0);
145 req->nr_version = NETMAP_API;
146 req->nr_flags = NR_REG_ALL_NIC;
149 req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
151 req->nr_flags = NR_REG_ALL_NIC;
153 req->nr_flags |= NR_ACCEPT_VNET_HDR;
154 snprintf (req->nr_name, IFNAMSIZ, "%s", if_name);
155 req->nr_name[IFNAMSIZ - 1] = 0;
157 if (ioctl (nif->fd, NIOCREGIF, req))
159 ret = VNET_API_ERROR_NOT_CONNECTED;
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)
168 reg->mem = mmap (NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
170 clib_warning ("mem %p", reg->mem);
171 if (reg->mem == MAP_FAILED)
173 ret = VNET_API_ERROR_NOT_CONNECTED;
176 reg->region_size = req->nr_memsize;
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;
188 if (tm->n_vlib_mains > 1)
190 nif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
191 CLIB_CACHE_LINE_BYTES);
192 memset ((void *) nif->lockp, 0, CLIB_CACHE_LINE_BYTES);
196 unix_file_t template = { 0 };
197 template.read_function = netmap_fd_read_ready;
198 template.file_descriptor = nif->fd;
199 template.private_data = nif->if_index;
200 nif->unix_file_index = unix_file_add (&unix_main, &template);
203 /*use configured or generate random MAC address */
205 memcpy (hw_addr, hw_addr_set, 6);
208 f64 now = vlib_time_now (vm);
210 rnd = (u32) (now * 1e6);
211 rnd = random_u32 (&rnd);
213 memcpy (hw_addr + 2, &rnd, sizeof (rnd));
218 error = ethernet_register_interface (vnm, netmap_device_class.index,
219 nif->if_index, hw_addr,
221 netmap_eth_flag_change);
225 clib_error_report (error);
226 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
230 sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
231 nif->sw_if_index = sw->sw_if_index;
233 mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
236 *sw_if_index = nif->sw_if_index;
238 if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 1)
239 netmap_worker_thread_enable ();
244 close_netmap_if (nm, nif);
249 netmap_delete_if (vlib_main_t * vm, u8 * host_if_name)
251 vnet_main_t *vnm = vnet_get_main ();
252 netmap_main_t *nm = &netmap_main;
255 vlib_thread_main_t *tm = vlib_get_thread_main ();
257 p = mhash_get (&nm->if_index_by_host_if_name, host_if_name);
260 clib_warning ("Host interface %s does not exist", host_if_name);
261 return VNET_API_ERROR_SYSCALL_ERROR_1;
263 nif = pool_elt_at_index (nm->interfaces, p[0]);
265 /* bring down the interface */
266 vnet_hw_interface_set_flags (vnm, nif->hw_if_index, 0);
268 ethernet_delete_interface (vnm, nif->hw_if_index);
270 close_netmap_if (nm, nif);
272 if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 0)
273 netmap_worker_thread_disable ();
278 static clib_error_t *
279 netmap_init (vlib_main_t * vm)
281 netmap_main_t *nm = &netmap_main;
282 vlib_thread_main_t *tm = vlib_get_thread_main ();
283 vlib_thread_registration_t *tr;
286 memset (nm, 0, sizeof (netmap_main_t));
288 nm->input_cpu_first_index = 0;
289 nm->input_cpu_count = 1;
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;
295 if (tr && tr->count > 0)
297 nm->input_cpu_first_index = tr->first_index;
298 nm->input_cpu_count = tr->count;
301 mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
303 vec_validate_aligned (nm->rx_buffers, tm->n_vlib_mains - 1,
304 CLIB_CACHE_LINE_BYTES);
309 VLIB_INIT_FUNCTION (netmap_init);
312 * fd.io coding-style-patch-verification: ON
315 * eval: (c-set-style "gnu")