blob: 7cab6bb0289521b21046858a6fe5d527cfe1cacf [file] [log] [blame]
Damjan Marion108c7312016-04-20 05:04:20 +02001/*
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>
23#include <vnet/devices/netmap/net_netmap.h>
24
25#include <vlib/vlib.h>
26#include <vlib/unix/unix.h>
27#include <vnet/ethernet/ethernet.h>
28#include <vnet/devices/netmap/netmap.h>
29
Dave Wallace71612d62017-10-24 01:32:41 -040030netmap_main_t netmap_main;
31
Damjan Marion108c7312016-04-20 05:04:20 +020032static u32
Damjan Marion00a9dca2016-08-17 17:05:46 +020033netmap_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
34 u32 flags)
Damjan Marion108c7312016-04-20 05:04:20 +020035{
36 /* nothing for now */
37 return 0;
38}
39
Damjan Marion00a9dca2016-08-17 17:05:46 +020040static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +020041netmap_fd_read_ready (clib_file_t * uf)
Damjan Marion108c7312016-04-20 05:04:20 +020042{
Damjan Marion00a9dca2016-08-17 17:05:46 +020043 vlib_main_t *vm = vlib_get_main ();
44 netmap_main_t *nm = &netmap_main;
Damjan Marion108c7312016-04-20 05:04:20 +020045 u32 idx = uf->private_data;
46
Damjan Marion00a9dca2016-08-17 17:05:46 +020047 nm->pending_input_bitmap =
48 clib_bitmap_set (nm->pending_input_bitmap, idx, 1);
Damjan Marion108c7312016-04-20 05:04:20 +020049
50 /* Schedule the rx node */
51 vlib_node_set_interrupt_pending (vm, netmap_input_node.index);
52
53 return 0;
54}
55
56static void
Damjan Marion00a9dca2016-08-17 17:05:46 +020057close_netmap_if (netmap_main_t * nm, netmap_if_t * nif)
Damjan Marion108c7312016-04-20 05:04:20 +020058{
Damjan Marion56dd5432017-09-08 19:52:02 +020059 if (nif->clib_file_index != ~0)
Damjan Marion00a9dca2016-08-17 17:05:46 +020060 {
Damjan Marion56dd5432017-09-08 19:52:02 +020061 clib_file_del (&file_main, file_main.file_pool + nif->clib_file_index);
62 nif->clib_file_index = ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +020063 }
Eyal Barif298ecf2016-09-19 18:47:39 +030064 else if (nif->fd > -1)
Damjan Marion00a9dca2016-08-17 17:05:46 +020065 close (nif->fd);
Damjan Marion108c7312016-04-20 05:04:20 +020066
67 if (nif->mem_region)
68 {
Damjan Marion00a9dca2016-08-17 17:05:46 +020069 netmap_mem_region_t *reg = &nm->mem_regions[nif->mem_region];
Damjan Marion108c7312016-04-20 05:04:20 +020070 if (--reg->refcnt == 0)
71 {
Damjan Marion00a9dca2016-08-17 17:05:46 +020072 munmap (reg->mem, reg->region_size);
Damjan Marion108c7312016-04-20 05:04:20 +020073 reg->region_size = 0;
74 }
75 }
76
77
Damjan Marion00a9dca2016-08-17 17:05:46 +020078 mhash_unset (&nm->if_index_by_host_if_name, nif->host_if_name,
79 &nif->if_index);
80 vec_free (nif->host_if_name);
81 vec_free (nif->req);
Damjan Marion108c7312016-04-20 05:04:20 +020082
Dave Barachb7b92992018-10-17 10:38:51 -040083 clib_memset (nif, 0, sizeof (*nif));
Damjan Marion00a9dca2016-08-17 17:05:46 +020084 pool_put (nm->interfaces, nif);
Damjan Marion108c7312016-04-20 05:04:20 +020085}
86
87int
Damjan Marion00a9dca2016-08-17 17:05:46 +020088netmap_worker_thread_enable ()
Mohsin KAZMI64f450d2016-07-06 03:17:50 +020089{
90 /* if worker threads are enabled, switch to polling mode */
Damjan Marion00a9dca2016-08-17 17:05:46 +020091 foreach_vlib_main ((
92 {
93 vlib_node_set_state (this_vlib_main,
94 netmap_input_node.index,
95 VLIB_NODE_STATE_POLLING);
96 }));
Mohsin KAZMI64f450d2016-07-06 03:17:50 +020097
98 return 0;
99}
100
101int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200102netmap_worker_thread_disable ()
Mohsin KAZMI64f450d2016-07-06 03:17:50 +0200103{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200104 foreach_vlib_main ((
105 {
106 vlib_node_set_state (this_vlib_main,
107 netmap_input_node.index,
108 VLIB_NODE_STATE_INTERRUPT);
109 }));
Mohsin KAZMI64f450d2016-07-06 03:17:50 +0200110
111 return 0;
112}
113
114int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200115netmap_create_if (vlib_main_t * vm, u8 * if_name, u8 * hw_addr_set,
116 u8 is_pipe, u8 is_master, u32 * sw_if_index)
Damjan Marion108c7312016-04-20 05:04:20 +0200117{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200118 netmap_main_t *nm = &netmap_main;
Damjan Marion108c7312016-04-20 05:04:20 +0200119 int ret = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200120 netmap_if_t *nif = 0;
Damjan Marion108c7312016-04-20 05:04:20 +0200121 u8 hw_addr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200122 clib_error_t *error = 0;
123 vnet_sw_interface_t *sw;
124 vnet_main_t *vnm = vnet_get_main ();
125 uword *p;
126 struct nmreq *req = 0;
127 netmap_mem_region_t *reg;
128 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion108c7312016-04-20 05:04:20 +0200129 int fd;
130
131 p = mhash_get (&nm->if_index_by_host_if_name, if_name);
132 if (p)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200133 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
Damjan Marion108c7312016-04-20 05:04:20 +0200134
Damjan Marion00a9dca2016-08-17 17:05:46 +0200135 fd = open ("/dev/netmap", O_RDWR);
Damjan Marion108c7312016-04-20 05:04:20 +0200136 if (fd < 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200137 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
Damjan Marion108c7312016-04-20 05:04:20 +0200138
139 pool_get (nm->interfaces, nif);
140 nif->if_index = nif - nm->interfaces;
141 nif->fd = fd;
Damjan Marion56dd5432017-09-08 19:52:02 +0200142 nif->clib_file_index = ~0;
Damjan Marion108c7312016-04-20 05:04:20 +0200143
Damjan Marion00a9dca2016-08-17 17:05:46 +0200144 vec_validate (req, 0);
Damjan Marion108c7312016-04-20 05:04:20 +0200145 nif->req = req;
146 req->nr_version = NETMAP_API;
147 req->nr_flags = NR_REG_ALL_NIC;
148
149 if (is_pipe)
150 req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
151 else
152 req->nr_flags = NR_REG_ALL_NIC;
153
154 req->nr_flags |= NR_ACCEPT_VNET_HDR;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200155 snprintf (req->nr_name, IFNAMSIZ, "%s", if_name);
156 req->nr_name[IFNAMSIZ - 1] = 0;
Damjan Marion108c7312016-04-20 05:04:20 +0200157
Damjan Marion00a9dca2016-08-17 17:05:46 +0200158 if (ioctl (nif->fd, NIOCREGIF, req))
Damjan Marion108c7312016-04-20 05:04:20 +0200159 {
160 ret = VNET_API_ERROR_NOT_CONNECTED;
161 goto error;
162 }
163
164 nif->mem_region = req->nr_arg2;
165 vec_validate (nm->mem_regions, nif->mem_region);
166 reg = &nm->mem_regions[nif->mem_region];
167 if (reg->region_size == 0)
168 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200169 reg->mem = mmap (NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
170 MAP_SHARED, fd, 0);
171 clib_warning ("mem %p", reg->mem);
Damjan Marion108c7312016-04-20 05:04:20 +0200172 if (reg->mem == MAP_FAILED)
173 {
174 ret = VNET_API_ERROR_NOT_CONNECTED;
175 goto error;
176 }
177 reg->region_size = req->nr_memsize;
178 }
179 reg->refcnt++;
180
Damjan Marion00a9dca2016-08-17 17:05:46 +0200181 nif->nifp = NETMAP_IF (reg->mem, req->nr_offset);
Damjan Marion108c7312016-04-20 05:04:20 +0200182 nif->first_rx_ring = 0;
183 nif->last_rx_ring = 0;
184 nif->first_tx_ring = 0;
185 nif->last_tx_ring = 0;
186 nif->host_if_name = if_name;
187 nif->per_interface_next_index = ~0;
188
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200189 if (tm->n_vlib_mains > 1)
Damjan Marion1927da22017-03-27 17:08:20 +0200190 clib_spinlock_init (&nif->lockp);
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200191
Damjan Marion108c7312016-04-20 05:04:20 +0200192 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200193 clib_file_t template = { 0 };
Damjan Marion108c7312016-04-20 05:04:20 +0200194 template.read_function = netmap_fd_read_ready;
195 template.file_descriptor = nif->fd;
196 template.private_data = nif->if_index;
Paul Vinciguerra5481ad42020-01-28 14:47:17 -0500197 template.description = format (0, "netmap socket");
Damjan Marion56dd5432017-09-08 19:52:02 +0200198 nif->clib_file_index = clib_file_add (&file_main, &template);
Damjan Marion108c7312016-04-20 05:04:20 +0200199 }
200
201 /*use configured or generate random MAC address */
202 if (hw_addr_set)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200203 memcpy (hw_addr, hw_addr_set, 6);
Damjan Marion108c7312016-04-20 05:04:20 +0200204 else
205 {
Damjan Marion00a9dca2016-08-17 17:05:46 +0200206 f64 now = vlib_time_now (vm);
Damjan Marion108c7312016-04-20 05:04:20 +0200207 u32 rnd;
208 rnd = (u32) (now * 1e6);
209 rnd = random_u32 (&rnd);
210
Damjan Marion00a9dca2016-08-17 17:05:46 +0200211 memcpy (hw_addr + 2, &rnd, sizeof (rnd));
Damjan Marion108c7312016-04-20 05:04:20 +0200212 hw_addr[0] = 2;
213 hw_addr[1] = 0xfe;
214 }
215
Damjan Marion00a9dca2016-08-17 17:05:46 +0200216 error = ethernet_register_interface (vnm, netmap_device_class.index,
217 nif->if_index, hw_addr,
218 &nif->hw_if_index,
219 netmap_eth_flag_change);
Damjan Marion108c7312016-04-20 05:04:20 +0200220
221 if (error)
222 {
223 clib_error_report (error);
224 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
225 goto error;
226 }
227
228 sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
229 nif->sw_if_index = sw->sw_if_index;
230
Damjan Marion108c7312016-04-20 05:04:20 +0200231 mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
232
Pierre Pfister78ea9c22016-05-23 12:51:54 +0100233 if (sw_if_index)
234 *sw_if_index = nif->sw_if_index;
235
Damjan Marion00a9dca2016-08-17 17:05:46 +0200236 if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 1)
237 netmap_worker_thread_enable ();
Mohsin KAZMI64f450d2016-07-06 03:17:50 +0200238
Damjan Marion108c7312016-04-20 05:04:20 +0200239 return 0;
240
241error:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200242 close_netmap_if (nm, nif);
Damjan Marion108c7312016-04-20 05:04:20 +0200243 return ret;
244}
245
246int
Damjan Marion00a9dca2016-08-17 17:05:46 +0200247netmap_delete_if (vlib_main_t * vm, u8 * host_if_name)
Damjan Marion108c7312016-04-20 05:04:20 +0200248{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200249 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion108c7312016-04-20 05:04:20 +0200250 netmap_main_t *nm = &netmap_main;
251 netmap_if_t *nif;
252 uword *p;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200253 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion108c7312016-04-20 05:04:20 +0200254
Damjan Marion00a9dca2016-08-17 17:05:46 +0200255 p = mhash_get (&nm->if_index_by_host_if_name, host_if_name);
256 if (p == NULL)
257 {
258 clib_warning ("Host interface %s does not exist", host_if_name);
259 return VNET_API_ERROR_SYSCALL_ERROR_1;
260 }
261 nif = pool_elt_at_index (nm->interfaces, p[0]);
Damjan Marion108c7312016-04-20 05:04:20 +0200262
263 /* bring down the interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200264 vnet_hw_interface_set_flags (vnm, nif->hw_if_index, 0);
Damjan Marion108c7312016-04-20 05:04:20 +0200265
Damjan Marion00a9dca2016-08-17 17:05:46 +0200266 ethernet_delete_interface (vnm, nif->hw_if_index);
Damjan Marion108c7312016-04-20 05:04:20 +0200267
Damjan Marion00a9dca2016-08-17 17:05:46 +0200268 close_netmap_if (nm, nif);
Mohsin KAZMI64f450d2016-07-06 03:17:50 +0200269
Damjan Marion00a9dca2016-08-17 17:05:46 +0200270 if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 0)
271 netmap_worker_thread_disable ();
Mohsin KAZMI64f450d2016-07-06 03:17:50 +0200272
Damjan Marion108c7312016-04-20 05:04:20 +0200273 return 0;
274}
275
276static clib_error_t *
277netmap_init (vlib_main_t * vm)
278{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200279 netmap_main_t *nm = &netmap_main;
280 vlib_thread_main_t *tm = vlib_get_thread_main ();
281 vlib_thread_registration_t *tr;
282 uword *p;
Damjan Marion108c7312016-04-20 05:04:20 +0200283
Dave Barachb7b92992018-10-17 10:38:51 -0400284 clib_memset (nm, 0, sizeof (netmap_main_t));
Damjan Marion108c7312016-04-20 05:04:20 +0200285
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200286 nm->input_cpu_first_index = 0;
287 nm->input_cpu_count = 1;
288
289 /* find out which cpus will be used for input */
290 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
291 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
292
293 if (tr && tr->count > 0)
294 {
295 nm->input_cpu_first_index = tr->first_index;
296 nm->input_cpu_count = tr->count;
297 }
298
Damjan Marion108c7312016-04-20 05:04:20 +0200299 mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
300
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200301 vec_validate_aligned (nm->rx_buffers, tm->n_vlib_mains - 1,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200302 CLIB_CACHE_LINE_BYTES);
Mohsin KAZMIa7575ea2016-06-16 06:58:34 +0200303
Damjan Marion108c7312016-04-20 05:04:20 +0200304 return 0;
305}
306
307VLIB_INIT_FUNCTION (netmap_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200308
309/*
310 * fd.io coding-style-patch-verification: ON
311 *
312 * Local Variables:
313 * eval: (c-set-style "gnu")
314 * End:
315 */