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)
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;
97 p = mhash_get (&nm->if_index_by_host_if_name, if_name);
99 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
101 fd = open("/dev/netmap", O_RDWR);
103 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
105 pool_get (nm->interfaces, nif);
106 nif->if_index = nif - nm->interfaces;
108 nif->unix_file_index = ~0;
110 vec_validate(req, 0);
112 req->nr_version = NETMAP_API;
113 req->nr_flags = NR_REG_ALL_NIC;
116 req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
118 req->nr_flags = NR_REG_ALL_NIC;
120 req->nr_flags |= NR_ACCEPT_VNET_HDR;
121 snprintf(req->nr_name, IFNAMSIZ, "%s", if_name);
122 req->nr_name[IFNAMSIZ-1] = 0;
124 if (ioctl(nif->fd, NIOCREGIF, req))
126 ret = VNET_API_ERROR_NOT_CONNECTED;
130 nif->mem_region = req->nr_arg2;
131 vec_validate (nm->mem_regions, nif->mem_region);
132 reg = &nm->mem_regions[nif->mem_region];
133 if (reg->region_size == 0)
135 reg->mem = mmap(NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
137 clib_warning("mem %p", reg->mem);
138 if (reg->mem == MAP_FAILED)
140 ret = VNET_API_ERROR_NOT_CONNECTED;
143 reg->region_size = req->nr_memsize;
147 nif->nifp = NETMAP_IF(reg->mem, req->nr_offset);
148 nif->first_rx_ring = 0;
149 nif->last_rx_ring = 0;
150 nif->first_tx_ring = 0;
151 nif->last_tx_ring = 0;
152 nif->host_if_name = if_name;
153 nif->per_interface_next_index = ~0;
156 unix_file_t template = {0};
157 template.read_function = netmap_fd_read_ready;
158 template.file_descriptor = nif->fd;
159 template.private_data = nif->if_index;
160 nif->unix_file_index = unix_file_add (&unix_main, &template);
163 /*use configured or generate random MAC address */
165 memcpy(hw_addr, hw_addr_set, 6);
168 f64 now = vlib_time_now(vm);
170 rnd = (u32) (now * 1e6);
171 rnd = random_u32 (&rnd);
173 memcpy (hw_addr+2, &rnd, sizeof(rnd));
178 error = ethernet_register_interface(vnm, netmap_device_class.index,
179 nif->if_index, hw_addr, &nif->hw_if_index,
180 netmap_eth_flag_change);
184 clib_error_report (error);
185 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
189 sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
190 nif->sw_if_index = sw->sw_if_index;
192 vnet_hw_interface_set_flags (vnm, nif->hw_if_index,
193 VNET_HW_INTERFACE_FLAG_LINK_UP);
195 mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
200 close_netmap_if(nm, nif);
205 netmap_delete_if(vlib_main_t *vm, u8 *host_if_name)
207 vnet_main_t *vnm = vnet_get_main();
208 netmap_main_t *nm = &netmap_main;
212 p = mhash_get(&nm->if_index_by_host_if_name, host_if_name);
214 clib_warning("Host interface %s does not exist", host_if_name);
215 return VNET_API_ERROR_SYSCALL_ERROR_1;
217 nif = pool_elt_at_index(nm->interfaces, p[0]);
219 /* bring down the interface */
220 vnet_hw_interface_set_flags(vnm, nif->hw_if_index, 0);
222 ethernet_delete_interface(vnm, nif->hw_if_index);
224 close_netmap_if(nm, nif);
228 static clib_error_t *
229 netmap_init (vlib_main_t * vm)
231 netmap_main_t * nm = &netmap_main;
233 memset (nm, 0, sizeof (netmap_main_t));
235 mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
240 VLIB_INIT_FUNCTION (netmap_init);