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