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