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, u32 flags)
37 static clib_error_t * netmap_fd_read_ready (unix_file_t * uf)
39 vlib_main_t * vm = vlib_get_main();
40 netmap_main_t * nm = &netmap_main;
41 u32 idx = uf->private_data;
43 nm->pending_input_bitmap = clib_bitmap_set (nm->pending_input_bitmap, idx, 1);
45 /* Schedule the rx node */
46 vlib_node_set_interrupt_pending (vm, netmap_input_node.index);
52 close_netmap_if(netmap_main_t * nm, netmap_if_t * nif)
54 if (nif->unix_file_index != ~0) {
55 unix_file_del(&unix_main, unix_main.file_pool + nif->unix_file_index);
56 nif->unix_file_index = ~0;
64 netmap_mem_region_t * reg = &nm->mem_regions[nif->mem_region];
65 if (--reg->refcnt == 0)
67 munmap(reg->mem, reg->region_size);
73 mhash_unset(&nm->if_index_by_host_if_name, nif->host_if_name, &nif->if_index);
74 vec_free(nif->host_if_name);
77 memset(nif, 0, sizeof(*nif));
78 pool_put(nm->interfaces, nif);
82 netmap_create_if(vlib_main_t * vm, u8 * if_name, u8 * hw_addr_set,
83 u8 is_pipe, u8 is_master, u32 *sw_if_index)
85 netmap_main_t * nm = &netmap_main;
87 netmap_if_t * nif = 0;
89 clib_error_t * error = 0;
90 vnet_sw_interface_t * sw;
91 vnet_main_t *vnm = vnet_get_main();
93 struct nmreq * req = 0;
94 netmap_mem_region_t * reg;
95 vlib_thread_main_t * tm = vlib_get_thread_main();
98 p = mhash_get (&nm->if_index_by_host_if_name, if_name);
100 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
102 fd = open("/dev/netmap", O_RDWR);
104 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
106 pool_get (nm->interfaces, nif);
107 nif->if_index = nif - nm->interfaces;
109 nif->unix_file_index = ~0;
111 vec_validate(req, 0);
113 req->nr_version = NETMAP_API;
114 req->nr_flags = NR_REG_ALL_NIC;
117 req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
119 req->nr_flags = NR_REG_ALL_NIC;
121 req->nr_flags |= NR_ACCEPT_VNET_HDR;
122 snprintf(req->nr_name, IFNAMSIZ, "%s", if_name);
123 req->nr_name[IFNAMSIZ-1] = 0;
125 if (ioctl(nif->fd, NIOCREGIF, req))
127 ret = VNET_API_ERROR_NOT_CONNECTED;
131 nif->mem_region = req->nr_arg2;
132 vec_validate (nm->mem_regions, nif->mem_region);
133 reg = &nm->mem_regions[nif->mem_region];
134 if (reg->region_size == 0)
136 reg->mem = mmap(NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
138 clib_warning("mem %p", reg->mem);
139 if (reg->mem == MAP_FAILED)
141 ret = VNET_API_ERROR_NOT_CONNECTED;
144 reg->region_size = req->nr_memsize;
148 nif->nifp = NETMAP_IF(reg->mem, req->nr_offset);
149 nif->first_rx_ring = 0;
150 nif->last_rx_ring = 0;
151 nif->first_tx_ring = 0;
152 nif->last_tx_ring = 0;
153 nif->host_if_name = if_name;
154 nif->per_interface_next_index = ~0;
156 if (tm->n_vlib_mains > 1)
158 nif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
159 CLIB_CACHE_LINE_BYTES);
160 memset ((void *) nif->lockp, 0, CLIB_CACHE_LINE_BYTES);
164 unix_file_t template = {0};
165 template.read_function = netmap_fd_read_ready;
166 template.file_descriptor = nif->fd;
167 template.private_data = nif->if_index;
168 nif->unix_file_index = unix_file_add (&unix_main, &template);
171 /*use configured or generate random MAC address */
173 memcpy(hw_addr, hw_addr_set, 6);
176 f64 now = vlib_time_now(vm);
178 rnd = (u32) (now * 1e6);
179 rnd = random_u32 (&rnd);
181 memcpy (hw_addr+2, &rnd, sizeof(rnd));
186 error = ethernet_register_interface(vnm, netmap_device_class.index,
187 nif->if_index, hw_addr, &nif->hw_if_index,
188 netmap_eth_flag_change);
192 clib_error_report (error);
193 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
197 sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
198 nif->sw_if_index = sw->sw_if_index;
200 mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
203 *sw_if_index = nif->sw_if_index;
208 close_netmap_if(nm, nif);
213 netmap_delete_if(vlib_main_t *vm, u8 *host_if_name)
215 vnet_main_t *vnm = vnet_get_main();
216 netmap_main_t *nm = &netmap_main;
220 p = mhash_get(&nm->if_index_by_host_if_name, host_if_name);
222 clib_warning("Host interface %s does not exist", host_if_name);
223 return VNET_API_ERROR_SYSCALL_ERROR_1;
225 nif = pool_elt_at_index(nm->interfaces, p[0]);
227 /* bring down the interface */
228 vnet_hw_interface_set_flags(vnm, nif->hw_if_index, 0);
230 ethernet_delete_interface(vnm, nif->hw_if_index);
232 close_netmap_if(nm, nif);
236 static clib_error_t *
237 netmap_init (vlib_main_t * vm)
239 netmap_main_t * nm = &netmap_main;
240 vlib_thread_main_t * tm = vlib_get_thread_main();
241 vlib_thread_registration_t * tr;
244 memset (nm, 0, sizeof (netmap_main_t));
246 nm->input_cpu_first_index = 0;
247 nm->input_cpu_count = 1;
249 /* find out which cpus will be used for input */
250 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
251 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
253 if (tr && tr->count > 0)
255 nm->input_cpu_first_index = tr->first_index;
256 nm->input_cpu_count = tr->count;
259 /* if worker threads are enabled, switch to polling mode */
260 if (tm->n_vlib_mains > 1)
263 vlib_node_set_state(this_vlib_main, netmap_input_node.index, VLIB_NODE_STATE_POLLING);
266 mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
268 vec_validate_aligned (nm->rx_buffers, tm->n_vlib_mains - 1,
269 CLIB_CACHE_LINE_BYTES);
274 VLIB_INIT_FUNCTION (netmap_init);