Netmap: Resolve interface state issue
[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_create_if(vlib_main_t * vm, u8 * if_name, u8 * hw_addr_set,
83                  u8 is_pipe, u8 is_master, u32 *sw_if_index)
84 {
85   netmap_main_t * nm = &netmap_main;
86   int ret = 0;
87   netmap_if_t * nif = 0;
88   u8 hw_addr[6];
89   clib_error_t * error = 0;
90   vnet_sw_interface_t * sw;
91   vnet_main_t *vnm = vnet_get_main();
92   uword * p;
93   struct nmreq * req = 0;
94   netmap_mem_region_t * reg;
95   vlib_thread_main_t * tm = vlib_get_thread_main();
96   int fd;
97
98   p = mhash_get (&nm->if_index_by_host_if_name, if_name);
99   if (p)
100       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
101
102   fd = open("/dev/netmap", O_RDWR);
103   if (fd < 0)
104       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
105
106   pool_get (nm->interfaces, nif);
107   nif->if_index = nif - nm->interfaces;
108   nif->fd = fd;
109   nif->unix_file_index = ~0;
110
111   vec_validate(req, 0);
112   nif->req = req;
113   req->nr_version = NETMAP_API;
114   req->nr_flags = NR_REG_ALL_NIC;
115
116   if (is_pipe)
117     req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
118   else
119     req->nr_flags = NR_REG_ALL_NIC;
120
121   req->nr_flags |= NR_ACCEPT_VNET_HDR;
122   snprintf(req->nr_name, IFNAMSIZ, "%s", if_name);
123   req->nr_name[IFNAMSIZ-1] = 0;
124
125   if (ioctl(nif->fd, NIOCREGIF, req))
126     {
127       ret = VNET_API_ERROR_NOT_CONNECTED;
128       goto error;
129     }
130
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)
135     {
136       reg->mem = mmap(NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
137                       MAP_SHARED, fd, 0);
138       clib_warning("mem %p", reg->mem);
139       if (reg->mem == MAP_FAILED)
140         {
141           ret = VNET_API_ERROR_NOT_CONNECTED;
142           goto error;
143         }
144       reg->region_size = req->nr_memsize;
145     }
146   reg->refcnt++;
147
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;
155
156   if (tm->n_vlib_mains > 1)
157   {
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);
161   }
162
163   {
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);
169   }
170
171   /*use configured or generate random MAC address */
172   if (hw_addr_set)
173     memcpy(hw_addr, hw_addr_set, 6);
174   else
175     {
176       f64 now = vlib_time_now(vm);
177       u32 rnd;
178       rnd = (u32) (now * 1e6);
179       rnd = random_u32 (&rnd);
180
181       memcpy (hw_addr+2, &rnd, sizeof(rnd));
182       hw_addr[0] = 2;
183       hw_addr[1] = 0xfe;
184     }
185
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);
189
190   if (error)
191     {
192       clib_error_report (error);
193       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
194       goto error;
195     }
196
197   sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
198   nif->sw_if_index = sw->sw_if_index;
199
200   mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
201
202   if (sw_if_index)
203     *sw_if_index = nif->sw_if_index;
204
205   return 0;
206
207 error:
208   close_netmap_if(nm, nif);
209   return ret;
210 }
211
212 int
213 netmap_delete_if(vlib_main_t *vm, u8 *host_if_name)
214 {
215   vnet_main_t *vnm = vnet_get_main();
216   netmap_main_t *nm = &netmap_main;
217   netmap_if_t *nif;
218   uword *p;
219
220   p = mhash_get(&nm->if_index_by_host_if_name, host_if_name);
221   if (p == NULL) {
222     clib_warning("Host interface %s does not exist", host_if_name);
223     return VNET_API_ERROR_SYSCALL_ERROR_1;
224   }
225   nif = pool_elt_at_index(nm->interfaces, p[0]);
226
227   /* bring down the interface */
228   vnet_hw_interface_set_flags(vnm, nif->hw_if_index, 0);
229
230   ethernet_delete_interface(vnm, nif->hw_if_index);
231
232   close_netmap_if(nm, nif);
233   return 0;
234 }
235
236 static clib_error_t *
237 netmap_init (vlib_main_t * vm)
238 {
239   netmap_main_t * nm = &netmap_main;
240   vlib_thread_main_t * tm = vlib_get_thread_main();
241   vlib_thread_registration_t * tr;
242   uword * p;
243
244   memset (nm, 0, sizeof (netmap_main_t));
245
246   nm->input_cpu_first_index = 0;
247   nm->input_cpu_count = 1;
248
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;
252
253   if (tr && tr->count > 0)
254     {
255       nm->input_cpu_first_index = tr->first_index;
256       nm->input_cpu_count = tr->count;
257     }
258
259   /* if worker threads are enabled, switch to polling mode */
260   if (tm->n_vlib_mains > 1)
261     foreach_vlib_main (
262     ({
263         vlib_node_set_state(this_vlib_main, netmap_input_node.index, VLIB_NODE_STATE_POLLING);
264     }));
265
266   mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
267
268   vec_validate_aligned (nm->rx_buffers, tm->n_vlib_mains - 1,
269                         CLIB_CACHE_LINE_BYTES);
270
271   return 0;
272 }
273
274 VLIB_INIT_FUNCTION (netmap_init);