kni: identify device by name
[dpdk.git] / lib / librte_kni / rte_kni.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef RTE_EXEC_ENV_LINUXAPP
35 #error "KNI is not supported"
36 #endif
37
38 #include <string.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42
43 #include <rte_string_fns.h>
44 #include <rte_ethdev.h>
45 #include <rte_malloc.h>
46 #include <rte_log.h>
47 #include <rte_kni.h>
48 #include <rte_memzone.h>
49 #include <exec-env/rte_kni_common.h>
50 #include "rte_kni_fifo.h"
51
52 #define MAX_MBUF_BURST_NUM            32
53
54 /* Maximum number of ring entries */
55 #define KNI_FIFO_COUNT_MAX     1024
56 #define KNI_FIFO_SIZE          (KNI_FIFO_COUNT_MAX * sizeof(void *) + \
57                                         sizeof(struct rte_kni_fifo))
58
59 #define KNI_REQUEST_MBUF_NUM_MAX      32
60
61 #define KNI_MZ_CHECK(mz) do { if (mz) goto fail; } while (0)
62
63 /**
64  * KNI context
65  */
66 struct rte_kni {
67         char name[RTE_KNI_NAMESIZE];        /**< KNI interface name */
68         uint16_t group_id;                  /**< Group ID of KNI devices */
69         struct rte_mempool *pktmbuf_pool;   /**< pkt mbuf mempool */
70         unsigned mbuf_size;                 /**< mbuf size */
71
72         struct rte_kni_fifo *tx_q;          /**< TX queue */
73         struct rte_kni_fifo *rx_q;          /**< RX queue */
74         struct rte_kni_fifo *alloc_q;       /**< Allocated mbufs queue */
75         struct rte_kni_fifo *free_q;        /**< To be freed mbufs queue */
76
77         /* For request & response */
78         struct rte_kni_fifo *req_q;         /**< Request queue */
79         struct rte_kni_fifo *resp_q;        /**< Response queue */
80         void * sync_addr;                   /**< Req/Resp Mem address */
81
82         struct rte_kni_ops ops;             /**< operations for request */
83         uint8_t in_use : 1;                 /**< kni in use */
84 };
85
86 enum kni_ops_status {
87         KNI_REQ_NO_REGISTER = 0,
88         KNI_REQ_REGISTERED,
89 };
90
91 static void kni_free_mbufs(struct rte_kni *kni);
92 static void kni_allocate_mbufs(struct rte_kni *kni);
93
94 static volatile int kni_fd = -1;
95
96 static const struct rte_memzone *
97 kni_memzone_reserve(const char *name, size_t len, int socket_id,
98                                                 unsigned flags)
99 {
100         const struct rte_memzone *mz = rte_memzone_lookup(name);
101
102         if (mz == NULL)
103                 mz = rte_memzone_reserve(name, len, socket_id, flags);
104
105         return mz;
106 }
107
108 /* It is deprecated and just for backward compatibility */
109 struct rte_kni *
110 rte_kni_create(uint8_t port_id,
111                unsigned mbuf_size,
112                struct rte_mempool *pktmbuf_pool,
113                struct rte_kni_ops *ops)
114 {
115         struct rte_kni_conf conf;
116         struct rte_eth_dev_info info;
117
118         memset(&info, 0, sizeof(info));
119         memset(&conf, 0, sizeof(conf));
120         rte_eth_dev_info_get(port_id, &info);
121
122         rte_snprintf(conf.name, sizeof(conf.name), "vEth%u", port_id);
123         conf.addr = info.pci_dev->addr;
124         conf.id = info.pci_dev->id;
125         conf.group_id = (uint16_t)port_id;
126         conf.mbuf_size = mbuf_size;
127
128         /* Save the port id for request handling */
129         ops->port_id = port_id;
130
131         return rte_kni_alloc(pktmbuf_pool, &conf, ops);
132 }
133
134 struct rte_kni *
135 rte_kni_alloc(struct rte_mempool *pktmbuf_pool,
136               const struct rte_kni_conf *conf,
137               struct rte_kni_ops *ops)
138 {
139         int ret;
140         struct rte_kni_device_info dev_info;
141         struct rte_kni *ctx;
142         char intf_name[RTE_KNI_NAMESIZE];
143 #define OBJNAMSIZ 32
144         char obj_name[OBJNAMSIZ];
145         char mz_name[RTE_MEMZONE_NAMESIZE];
146         const struct rte_memzone *mz;
147
148         if (!pktmbuf_pool || !conf || !conf->name[0])
149                 return NULL;
150
151         /* Check FD and open once */
152         if (kni_fd < 0) {
153                 kni_fd = open("/dev/" KNI_DEVICE, O_RDWR);
154                 if (kni_fd < 0) {
155                         RTE_LOG(ERR, KNI, "Can not open /dev/%s\n",
156                                                         KNI_DEVICE);
157                         return NULL;
158                 }
159         }
160
161         rte_snprintf(intf_name, RTE_KNI_NAMESIZE, conf->name);
162         rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%s", intf_name);
163         mz = kni_memzone_reserve(mz_name, sizeof(struct rte_kni), 
164                                 SOCKET_ID_ANY, 0);
165         KNI_MZ_CHECK(mz == NULL);
166         ctx = mz->addr;
167
168         if (ctx->in_use) {
169                 RTE_LOG(ERR, KNI, "KNI %s is in use\n", ctx->name);
170                 goto fail;
171         }
172         memset(ctx, 0, sizeof(struct rte_kni));
173         if (ops)
174                 memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops));
175
176         memset(&dev_info, 0, sizeof(dev_info));
177         dev_info.bus = conf->addr.bus;
178         dev_info.devid = conf->addr.devid;
179         dev_info.function = conf->addr.function;
180         dev_info.vendor_id = conf->id.vendor_id;
181         dev_info.device_id = conf->id.device_id;
182         dev_info.group_id = conf->group_id;
183         dev_info.mbuf_size = conf->mbuf_size;
184
185         rte_snprintf(ctx->name, RTE_KNI_NAMESIZE, intf_name);
186         rte_snprintf(dev_info.name, RTE_KNI_NAMESIZE, intf_name);
187
188         RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
189                 dev_info.bus, dev_info.devid, dev_info.function,
190                         dev_info.vendor_id, dev_info.device_id);
191
192         /* TX RING */
193         rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%s", intf_name);
194         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
195         KNI_MZ_CHECK(mz == NULL);
196         ctx->tx_q = mz->addr;
197         kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
198         dev_info.tx_phys = mz->phys_addr;
199
200         /* RX RING */
201         rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%s", intf_name);
202         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
203         KNI_MZ_CHECK(mz == NULL);
204         ctx->rx_q = mz->addr;
205         kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
206         dev_info.rx_phys = mz->phys_addr;
207
208         /* ALLOC RING */
209         rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%s", intf_name);
210         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
211         KNI_MZ_CHECK(mz == NULL);
212         ctx->alloc_q = mz->addr;
213         kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
214         dev_info.alloc_phys = mz->phys_addr;
215
216         /* FREE RING */
217         rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%s", intf_name);
218         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
219         KNI_MZ_CHECK(mz == NULL);
220         ctx->free_q = mz->addr;
221         kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
222         dev_info.free_phys = mz->phys_addr;
223
224         /* Request RING */
225         rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%s", intf_name);
226         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
227         KNI_MZ_CHECK(mz == NULL);
228         ctx->req_q = mz->addr;
229         kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
230         dev_info.req_phys = mz->phys_addr;
231
232         /* Response RING */
233         rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%s", intf_name);
234         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
235         KNI_MZ_CHECK(mz == NULL);
236         ctx->resp_q = mz->addr;
237         kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
238         dev_info.resp_phys = mz->phys_addr;
239
240         /* Req/Resp sync mem area */
241         rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%s", intf_name);
242         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
243         KNI_MZ_CHECK(mz == NULL);
244         ctx->sync_addr = mz->addr;
245         dev_info.sync_va = mz->addr;
246         dev_info.sync_phys = mz->phys_addr;
247
248         /* MBUF mempool */
249         rte_snprintf(mz_name, sizeof(mz_name), "MP_%s", pktmbuf_pool->name);
250         mz = rte_memzone_lookup(mz_name);
251         KNI_MZ_CHECK(mz == NULL);
252         dev_info.mbuf_va = mz->addr;
253         dev_info.mbuf_phys = mz->phys_addr;
254         ctx->pktmbuf_pool = pktmbuf_pool;
255         ctx->group_id = conf->group_id;
256         ctx->mbuf_size = conf->mbuf_size;
257
258         ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
259         KNI_MZ_CHECK(ret < 0);
260
261         ctx->in_use = 1;
262
263         return ctx;
264
265 fail:
266
267         return NULL;
268 }
269
270 static void
271 kni_free_fifo(struct rte_kni_fifo *fifo)
272 {
273         int ret;
274         struct rte_mbuf *pkt;
275
276         do {
277                 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
278                 if (ret)
279                         rte_pktmbuf_free(pkt);
280         } while (ret);
281 }
282
283 int
284 rte_kni_release(struct rte_kni *kni)
285 {
286         struct rte_kni_device_info dev_info;
287
288         if (!kni || !kni->in_use)
289                 return -1;
290
291         rte_snprintf(dev_info.name, sizeof(dev_info.name), kni->name);
292         if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &dev_info) < 0) {
293                 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
294                 return -1;
295         }
296
297         /* mbufs in all fifo should be released, except request/response */
298         kni_free_fifo(kni->tx_q);
299         kni_free_fifo(kni->rx_q);
300         kni_free_fifo(kni->alloc_q);
301         kni_free_fifo(kni->free_q);
302         memset(kni, 0, sizeof(struct rte_kni));
303
304         return 0;
305 }
306
307 int
308 rte_kni_handle_request(struct rte_kni *kni)
309 {
310         unsigned ret;
311         struct rte_kni_request *req;
312
313         if (kni == NULL)
314                 return -1;
315
316         /* Get request mbuf */
317         ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
318         if (ret != 1)
319                 return 0; /* It is OK of can not getting the request mbuf */
320
321         if (req != kni->sync_addr) {
322                 rte_panic("Wrong req pointer %p\n", req);
323         }
324
325         /* Analyze the request and call the relevant actions for it */
326         switch (req->req_id) {
327         case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
328                 if (kni->ops.change_mtu)
329                         req->result = kni->ops.change_mtu(kni->ops.port_id,
330                                                         req->new_mtu);
331                 break;
332         case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
333                 if (kni->ops.config_network_if)
334                         req->result = kni->ops.config_network_if(\
335                                         kni->ops.port_id, req->if_up);
336                 break;
337         default:
338                 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
339                 req->result = -EINVAL;
340                 break;
341         }
342
343         /* Construct response mbuf and put it back to resp_q */
344         ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
345         if (ret != 1) {
346                 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
347                 return -1; /* It is an error of can't putting the mbuf back */
348         }
349
350         return 0;
351 }
352
353 unsigned
354 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
355 {
356         unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
357
358         /* Get mbufs from free_q and then free them */
359         kni_free_mbufs(kni);
360
361         return ret;
362 }
363
364 unsigned
365 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
366 {
367         unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
368
369         /* Allocate mbufs and then put them into alloc_q */
370         kni_allocate_mbufs(kni);
371
372         return ret;
373 }
374
375 static void
376 kni_free_mbufs(struct rte_kni *kni)
377 {
378         int i, ret;
379         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
380
381         ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
382         if (likely(ret > 0)) {
383                 for (i = 0; i < ret; i++)
384                         rte_pktmbuf_free(pkts[i]);
385         }
386 }
387
388 static void
389 kni_allocate_mbufs(struct rte_kni *kni)
390 {
391         int i, ret;
392         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
393
394         /* Check if pktmbuf pool has been configured */
395         if (kni->pktmbuf_pool == NULL) {
396                 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
397                 return;
398         }
399
400         for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
401                 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
402                 if (unlikely(pkts[i] == NULL)) {
403                         /* Out of memory */
404                         RTE_LOG(ERR, KNI, "Out of memory\n");
405                         break;
406                 }
407         }
408
409         /* No pkt mbuf alocated */
410         if (i <= 0)
411                 return;
412
413         ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
414
415         /* Check if any mbufs not put into alloc_q, and then free them */
416         if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
417                 int j;
418
419                 for (j = ret; j < i; j++)
420                         rte_pktmbuf_free(pkts[j]);
421         }
422 }
423
424 /* It is deprecated and just for backward compatibility */
425 uint8_t
426 rte_kni_get_port_id(struct rte_kni *kni)
427 {
428         if (!kni)
429                 return ~0x0;
430
431         return kni->ops.port_id;
432 }
433
434 struct rte_kni *
435 rte_kni_get(const char *name)
436 {
437         struct rte_kni *kni;
438         const struct rte_memzone *mz;
439         char mz_name[RTE_MEMZONE_NAMESIZE];
440
441         if (!name || !name[0])
442                 return NULL;
443
444         rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%s", name);
445         mz = rte_memzone_lookup(mz_name);
446         if (!mz)
447                 return NULL;
448
449         kni = mz->addr;
450         if (!kni->in_use)
451                 return NULL;
452
453         return kni;
454 }
455
456 /*
457  * It is deprecated and just for backward compatibility.
458  */
459 struct rte_kni *
460 rte_kni_info_get(uint8_t port_id)
461 {
462         char name[RTE_MEMZONE_NAMESIZE];
463
464         if (port_id >= RTE_MAX_ETHPORTS)
465                 return NULL;
466
467         rte_snprintf(name, RTE_MEMZONE_NAMESIZE, "vEth%u", port_id);
468
469         return rte_kni_get(name);
470 }
471
472 static enum kni_ops_status
473 kni_check_request_register(struct rte_kni_ops *ops)
474 {
475         /* check if KNI request ops has been registered*/
476         if( NULL == ops )
477                 return KNI_REQ_NO_REGISTER;
478                  
479         if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
480                 return KNI_REQ_NO_REGISTER;
481
482         return KNI_REQ_REGISTERED;
483 }
484
485 int
486 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
487 {
488         enum kni_ops_status req_status;
489         
490         if (NULL == ops) {
491                 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
492                 return -1;
493         }
494
495         if (NULL == kni) {
496                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
497                 return -1;
498         }
499
500         req_status = kni_check_request_register(&kni->ops);
501         if ( KNI_REQ_REGISTERED == req_status) {
502                 RTE_LOG(ERR, KNI, "The KNI request operation"
503                                         "has already registered.\n");
504                 return -1;
505         }
506
507         memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));     
508         return 0;
509 }
510
511 int
512 rte_kni_unregister_handlers(struct rte_kni *kni)
513 {
514         if (NULL == kni) {
515                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
516                 return -1;
517         }
518         
519         kni->ops.change_mtu = NULL;
520         kni->ops.config_network_if = NULL;
521         return 0;
522 }