df3b2be499e25e2c66ac2858149904d462e8a53a
[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   int fd;
96
97   p = mhash_get (&nm->if_index_by_host_if_name, if_name);
98   if (p)
99       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
100
101   fd = open("/dev/netmap", O_RDWR);
102   if (fd < 0)
103       return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
104
105   pool_get (nm->interfaces, nif);
106   nif->if_index = nif - nm->interfaces;
107   nif->fd = fd;
108   nif->unix_file_index = ~0;
109
110   vec_validate(req, 0);
111   nif->req = req;
112   req->nr_version = NETMAP_API;
113   req->nr_flags = NR_REG_ALL_NIC;
114
115   if (is_pipe)
116     req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
117   else
118     req->nr_flags = NR_REG_ALL_NIC;
119
120   req->nr_flags |= NR_ACCEPT_VNET_HDR;
121   snprintf(req->nr_name, IFNAMSIZ, "%s", if_name);
122   req->nr_name[IFNAMSIZ-1] = 0;
123
124   if (ioctl(nif->fd, NIOCREGIF, req))
125     {
126       ret = VNET_API_ERROR_NOT_CONNECTED;
127       goto error;
128     }
129
130   nif->mem_region = req->nr_arg2;
131   vec_validate (nm->mem_regions, nif->mem_region);
132   reg = &nm->mem_regions[nif->mem_region];
133   if (reg->region_size == 0)
134     {
135       reg->mem = mmap(NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
136                       MAP_SHARED, fd, 0);
137       clib_warning("mem %p", reg->mem);
138       if (reg->mem == MAP_FAILED)
139         {
140           ret = VNET_API_ERROR_NOT_CONNECTED;
141           goto error;
142         }
143       reg->region_size = req->nr_memsize;
144     }
145   reg->refcnt++;
146
147   nif->nifp = NETMAP_IF(reg->mem, req->nr_offset);
148   nif->first_rx_ring = 0;
149   nif->last_rx_ring = 0;
150   nif->first_tx_ring = 0;
151   nif->last_tx_ring = 0;
152   nif->host_if_name = if_name;
153   nif->per_interface_next_index = ~0;
154
155   {
156     unix_file_t template = {0};
157     template.read_function = netmap_fd_read_ready;
158     template.file_descriptor = nif->fd;
159     template.private_data = nif->if_index;
160     nif->unix_file_index = unix_file_add (&unix_main, &template);
161   }
162
163   /*use configured or generate random MAC address */
164   if (hw_addr_set)
165     memcpy(hw_addr, hw_addr_set, 6);
166   else
167     {
168       f64 now = vlib_time_now(vm);
169       u32 rnd;
170       rnd = (u32) (now * 1e6);
171       rnd = random_u32 (&rnd);
172
173       memcpy (hw_addr+2, &rnd, sizeof(rnd));
174       hw_addr[0] = 2;
175       hw_addr[1] = 0xfe;
176     }
177
178   error = ethernet_register_interface(vnm, netmap_device_class.index,
179                                       nif->if_index, hw_addr, &nif->hw_if_index,
180                                       netmap_eth_flag_change);
181
182   if (error)
183     {
184       clib_error_report (error);
185       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
186       goto error;
187     }
188
189   sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
190   nif->sw_if_index = sw->sw_if_index;
191
192   vnet_hw_interface_set_flags (vnm, nif->hw_if_index,
193                                VNET_HW_INTERFACE_FLAG_LINK_UP);
194
195   mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
196
197   if (sw_if_index)
198     *sw_if_index = nif->sw_if_index;
199
200   return 0;
201
202 error:
203   close_netmap_if(nm, nif);
204   return ret;
205 }
206
207 int
208 netmap_delete_if(vlib_main_t *vm, u8 *host_if_name)
209 {
210   vnet_main_t *vnm = vnet_get_main();
211   netmap_main_t *nm = &netmap_main;
212   netmap_if_t *nif;
213   uword *p;
214
215   p = mhash_get(&nm->if_index_by_host_if_name, host_if_name);
216   if (p == NULL) {
217     clib_warning("Host interface %s does not exist", host_if_name);
218     return VNET_API_ERROR_SYSCALL_ERROR_1;
219   }
220   nif = pool_elt_at_index(nm->interfaces, p[0]);
221
222   /* bring down the interface */
223   vnet_hw_interface_set_flags(vnm, nif->hw_if_index, 0);
224
225   ethernet_delete_interface(vnm, nif->hw_if_index);
226
227   close_netmap_if(nm, nif);
228   return 0;
229 }
230
231 static clib_error_t *
232 netmap_init (vlib_main_t * vm)
233 {
234   netmap_main_t * nm = &netmap_main;
235
236   memset (nm, 0, sizeof (netmap_main_t));
237
238   mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
239
240   return 0;
241 }
242
243 VLIB_INIT_FUNCTION (netmap_init);