7f1cadd795174e6e38d1fd5950ffc7f1cc184c5f
[vpp.git] / vnet / vnet / devices / netmap / netmap.c
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>
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
30 static u32
31 netmap_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
32 {
33   /* nothing for now */
34   return 0;
35 }
36
37 static clib_error_t * netmap_fd_read_ready (unix_file_t * uf)
38 {
39   vlib_main_t * vm = vlib_get_main();
40   netmap_main_t * nm = &netmap_main;
41   u32 idx = uf->private_data;
42
43   nm->pending_input_bitmap = clib_bitmap_set (nm->pending_input_bitmap, idx, 1);
44
45   /* Schedule the rx node */
46   vlib_node_set_interrupt_pending (vm, netmap_input_node.index);
47
48   return 0;
49 }
50
51 static void
52 close_netmap_if(netmap_main_t * nm, netmap_if_t * nif)
53 {
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;
57   }
58
59   if (nif->fd > -1)
60     close(nif->fd);
61
62   if (nif->mem_region)
63     {
64       netmap_mem_region_t * reg = &nm->mem_regions[nif->mem_region];
65       if (--reg->refcnt == 0)
66         {
67           munmap(reg->mem, reg->region_size);
68           reg->region_size = 0;
69         }
70     }
71
72
73   mhash_unset(&nm->if_index_by_host_if_name, nif->host_if_name, &nif->if_index);
74   vec_free(nif->host_if_name);
75   vec_free(nif->req);
76
77   memset(nif, 0, sizeof(*nif));
78   pool_put(nm->interfaces, nif);
79 }
80
81 int
82 netmap_worker_thread_enable()
83 {
84   /* if worker threads are enabled, switch to polling mode */
85   foreach_vlib_main (
86   ({
87       vlib_node_set_state(this_vlib_main, netmap_input_node.index, VLIB_NODE_STATE_POLLING);
88   }));
89
90   return 0;
91 }
92
93 int
94 netmap_worker_thread_disable()
95 {
96   foreach_vlib_main (
97   ({
98       vlib_node_set_state(this_vlib_main, netmap_input_node.index, VLIB_NODE_STATE_INTERRUPT);
99   }));
100
101   return 0;
102 }
103
104 int
105 netmap_create_if(vlib_main_t * vm, u8 * if_name, u8 * hw_addr_set,
106                  u8 is_pipe, u8 is_master, u32 *sw_if_index)
107 {
108   netmap_main_t * nm = &netmap_main;
109   int ret = 0;
110   netmap_if_t * nif = 0;
111   u8 hw_addr[6];
112   clib_error_t * error = 0;
113   vnet_sw_interface_t * sw;
114   vnet_main_t *vnm = vnet_get_main();
115   uword * p;
116   struct nmreq * req = 0;
117   netmap_mem_region_t * reg;
118   vlib_thread_main_t * tm = vlib_get_thread_main();
119   int fd;
120
121   p = mhash_get (&nm->if_index_by_host_if_name, if_name);
122   if (p)
123       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
124
125   fd = open("/dev/netmap", O_RDWR);
126   if (fd < 0)
127       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
128
129   pool_get (nm->interfaces, nif);
130   nif->if_index = nif - nm->interfaces;
131   nif->fd = fd;
132   nif->unix_file_index = ~0;
133
134   vec_validate(req, 0);
135   nif->req = req;
136   req->nr_version = NETMAP_API;
137   req->nr_flags = NR_REG_ALL_NIC;
138
139   if (is_pipe)
140     req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
141   else
142     req->nr_flags = NR_REG_ALL_NIC;
143
144   req->nr_flags |= NR_ACCEPT_VNET_HDR;
145   snprintf(req->nr_name, IFNAMSIZ, "%s", if_name);
146   req->nr_name[IFNAMSIZ-1] = 0;
147
148   if (ioctl(nif->fd, NIOCREGIF, req))
149     {
150       ret = VNET_API_ERROR_NOT_CONNECTED;
151       goto error;
152     }
153
154   nif->mem_region = req->nr_arg2;
155   vec_validate (nm->mem_regions, nif->mem_region);
156   reg = &nm->mem_regions[nif->mem_region];
157   if (reg->region_size == 0)
158     {
159       reg->mem = mmap(NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
160                       MAP_SHARED, fd, 0);
161       clib_warning("mem %p", reg->mem);
162       if (reg->mem == MAP_FAILED)
163         {
164           ret = VNET_API_ERROR_NOT_CONNECTED;
165           goto error;
166         }
167       reg->region_size = req->nr_memsize;
168     }
169   reg->refcnt++;
170
171   nif->nifp = NETMAP_IF(reg->mem, req->nr_offset);
172   nif->first_rx_ring = 0;
173   nif->last_rx_ring = 0;
174   nif->first_tx_ring = 0;
175   nif->last_tx_ring = 0;
176   nif->host_if_name = if_name;
177   nif->per_interface_next_index = ~0;
178
179   if (tm->n_vlib_mains > 1)
180   {
181     nif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
182                                          CLIB_CACHE_LINE_BYTES);
183     memset ((void *) nif->lockp, 0, CLIB_CACHE_LINE_BYTES);
184   }
185
186   {
187     unix_file_t template = {0};
188     template.read_function = netmap_fd_read_ready;
189     template.file_descriptor = nif->fd;
190     template.private_data = nif->if_index;
191     nif->unix_file_index = unix_file_add (&unix_main, &template);
192   }
193
194   /*use configured or generate random MAC address */
195   if (hw_addr_set)
196     memcpy(hw_addr, hw_addr_set, 6);
197   else
198     {
199       f64 now = vlib_time_now(vm);
200       u32 rnd;
201       rnd = (u32) (now * 1e6);
202       rnd = random_u32 (&rnd);
203
204       memcpy (hw_addr+2, &rnd, sizeof(rnd));
205       hw_addr[0] = 2;
206       hw_addr[1] = 0xfe;
207     }
208
209   error = ethernet_register_interface(vnm, netmap_device_class.index,
210                                       nif->if_index, hw_addr, &nif->hw_if_index,
211                                       netmap_eth_flag_change);
212
213   if (error)
214     {
215       clib_error_report (error);
216       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
217       goto error;
218     }
219
220   sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
221   nif->sw_if_index = sw->sw_if_index;
222
223   mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
224
225   if (sw_if_index)
226     *sw_if_index = nif->sw_if_index;
227
228   if (tm->n_vlib_mains > 1 && pool_elts(nm->interfaces) == 1)
229     netmap_worker_thread_enable();
230
231   return 0;
232
233 error:
234   close_netmap_if(nm, nif);
235   return ret;
236 }
237
238 int
239 netmap_delete_if(vlib_main_t *vm, u8 *host_if_name)
240 {
241   vnet_main_t *vnm = vnet_get_main();
242   netmap_main_t *nm = &netmap_main;
243   netmap_if_t *nif;
244   uword *p;
245   vlib_thread_main_t * tm = vlib_get_thread_main();
246
247   p = mhash_get(&nm->if_index_by_host_if_name, host_if_name);
248   if (p == NULL) {
249     clib_warning("Host interface %s does not exist", host_if_name);
250     return VNET_API_ERROR_SYSCALL_ERROR_1;
251   }
252   nif = pool_elt_at_index(nm->interfaces, p[0]);
253
254   /* bring down the interface */
255   vnet_hw_interface_set_flags(vnm, nif->hw_if_index, 0);
256
257   ethernet_delete_interface(vnm, nif->hw_if_index);
258
259   close_netmap_if(nm, nif);
260
261   if (tm->n_vlib_mains > 1 && pool_elts(nm->interfaces) == 0)
262     netmap_worker_thread_disable();
263
264   return 0;
265 }
266
267 static clib_error_t *
268 netmap_init (vlib_main_t * vm)
269 {
270   netmap_main_t * nm = &netmap_main;
271   vlib_thread_main_t * tm = vlib_get_thread_main();
272   vlib_thread_registration_t * tr;
273   uword * p;
274
275   memset (nm, 0, sizeof (netmap_main_t));
276
277   nm->input_cpu_first_index = 0;
278   nm->input_cpu_count = 1;
279
280   /* find out which cpus will be used for input */
281   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
282   tr = p ? (vlib_thread_registration_t *) p[0] : 0;
283
284   if (tr && tr->count > 0)
285     {
286       nm->input_cpu_first_index = tr->first_index;
287       nm->input_cpu_count = tr->count;
288     }
289
290   mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
291
292   vec_validate_aligned (nm->rx_buffers, tm->n_vlib_mains - 1,
293                         CLIB_CACHE_LINE_BYTES);
294
295   return 0;
296 }
297
298 VLIB_INIT_FUNCTION (netmap_init);