remove trailing whitespaces
[dpdk.git] / lib / librte_kni / rte_kni.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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.core_id = conf->core_id;
183         dev_info.force_bind = conf->force_bind;
184         dev_info.group_id = conf->group_id;
185         dev_info.mbuf_size = conf->mbuf_size;
186
187         rte_snprintf(ctx->name, RTE_KNI_NAMESIZE, intf_name);
188         rte_snprintf(dev_info.name, RTE_KNI_NAMESIZE, intf_name);
189
190         RTE_LOG(INFO, KNI, "pci: %02x:%02x:%02x \t %02x:%02x\n",
191                 dev_info.bus, dev_info.devid, dev_info.function,
192                         dev_info.vendor_id, dev_info.device_id);
193
194         /* TX RING */
195         rte_snprintf(obj_name, OBJNAMSIZ, "kni_tx_%s", intf_name);
196         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
197         KNI_MZ_CHECK(mz == NULL);
198         ctx->tx_q = mz->addr;
199         kni_fifo_init(ctx->tx_q, KNI_FIFO_COUNT_MAX);
200         dev_info.tx_phys = mz->phys_addr;
201
202         /* RX RING */
203         rte_snprintf(obj_name, OBJNAMSIZ, "kni_rx_%s", intf_name);
204         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
205         KNI_MZ_CHECK(mz == NULL);
206         ctx->rx_q = mz->addr;
207         kni_fifo_init(ctx->rx_q, KNI_FIFO_COUNT_MAX);
208         dev_info.rx_phys = mz->phys_addr;
209
210         /* ALLOC RING */
211         rte_snprintf(obj_name, OBJNAMSIZ, "kni_alloc_%s", intf_name);
212         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
213         KNI_MZ_CHECK(mz == NULL);
214         ctx->alloc_q = mz->addr;
215         kni_fifo_init(ctx->alloc_q, KNI_FIFO_COUNT_MAX);
216         dev_info.alloc_phys = mz->phys_addr;
217
218         /* FREE RING */
219         rte_snprintf(obj_name, OBJNAMSIZ, "kni_free_%s", intf_name);
220         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
221         KNI_MZ_CHECK(mz == NULL);
222         ctx->free_q = mz->addr;
223         kni_fifo_init(ctx->free_q, KNI_FIFO_COUNT_MAX);
224         dev_info.free_phys = mz->phys_addr;
225
226         /* Request RING */
227         rte_snprintf(obj_name, OBJNAMSIZ, "kni_req_%s", intf_name);
228         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
229         KNI_MZ_CHECK(mz == NULL);
230         ctx->req_q = mz->addr;
231         kni_fifo_init(ctx->req_q, KNI_FIFO_COUNT_MAX);
232         dev_info.req_phys = mz->phys_addr;
233
234         /* Response RING */
235         rte_snprintf(obj_name, OBJNAMSIZ, "kni_resp_%s", intf_name);
236         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
237         KNI_MZ_CHECK(mz == NULL);
238         ctx->resp_q = mz->addr;
239         kni_fifo_init(ctx->resp_q, KNI_FIFO_COUNT_MAX);
240         dev_info.resp_phys = mz->phys_addr;
241
242         /* Req/Resp sync mem area */
243         rte_snprintf(obj_name, OBJNAMSIZ, "kni_sync_%s", intf_name);
244         mz = kni_memzone_reserve(obj_name, KNI_FIFO_SIZE, SOCKET_ID_ANY, 0);
245         KNI_MZ_CHECK(mz == NULL);
246         ctx->sync_addr = mz->addr;
247         dev_info.sync_va = mz->addr;
248         dev_info.sync_phys = mz->phys_addr;
249
250         /* MBUF mempool */
251         rte_snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_OBJ_NAME,
252                 pktmbuf_pool->name);
253         mz = rte_memzone_lookup(mz_name);
254         KNI_MZ_CHECK(mz == NULL);
255         dev_info.mbuf_va = mz->addr;
256         dev_info.mbuf_phys = mz->phys_addr;
257         ctx->pktmbuf_pool = pktmbuf_pool;
258         ctx->group_id = conf->group_id;
259         ctx->mbuf_size = conf->mbuf_size;
260
261         ret = ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info);
262         KNI_MZ_CHECK(ret < 0);
263
264         ctx->in_use = 1;
265
266         return ctx;
267
268 fail:
269
270         return NULL;
271 }
272
273 static void
274 kni_free_fifo(struct rte_kni_fifo *fifo)
275 {
276         int ret;
277         struct rte_mbuf *pkt;
278
279         do {
280                 ret = kni_fifo_get(fifo, (void **)&pkt, 1);
281                 if (ret)
282                         rte_pktmbuf_free(pkt);
283         } while (ret);
284 }
285
286 int
287 rte_kni_release(struct rte_kni *kni)
288 {
289         struct rte_kni_device_info dev_info;
290
291         if (!kni || !kni->in_use)
292                 return -1;
293
294         rte_snprintf(dev_info.name, sizeof(dev_info.name), kni->name);
295         if (ioctl(kni_fd, RTE_KNI_IOCTL_RELEASE, &dev_info) < 0) {
296                 RTE_LOG(ERR, KNI, "Fail to release kni device\n");
297                 return -1;
298         }
299
300         /* mbufs in all fifo should be released, except request/response */
301         kni_free_fifo(kni->tx_q);
302         kni_free_fifo(kni->rx_q);
303         kni_free_fifo(kni->alloc_q);
304         kni_free_fifo(kni->free_q);
305         memset(kni, 0, sizeof(struct rte_kni));
306
307         return 0;
308 }
309
310 int
311 rte_kni_handle_request(struct rte_kni *kni)
312 {
313         unsigned ret;
314         struct rte_kni_request *req;
315
316         if (kni == NULL)
317                 return -1;
318
319         /* Get request mbuf */
320         ret = kni_fifo_get(kni->req_q, (void **)&req, 1);
321         if (ret != 1)
322                 return 0; /* It is OK of can not getting the request mbuf */
323
324         if (req != kni->sync_addr) {
325                 rte_panic("Wrong req pointer %p\n", req);
326         }
327
328         /* Analyze the request and call the relevant actions for it */
329         switch (req->req_id) {
330         case RTE_KNI_REQ_CHANGE_MTU: /* Change MTU */
331                 if (kni->ops.change_mtu)
332                         req->result = kni->ops.change_mtu(kni->ops.port_id,
333                                                         req->new_mtu);
334                 break;
335         case RTE_KNI_REQ_CFG_NETWORK_IF: /* Set network interface up/down */
336                 if (kni->ops.config_network_if)
337                         req->result = kni->ops.config_network_if(\
338                                         kni->ops.port_id, req->if_up);
339                 break;
340         default:
341                 RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id);
342                 req->result = -EINVAL;
343                 break;
344         }
345
346         /* Construct response mbuf and put it back to resp_q */
347         ret = kni_fifo_put(kni->resp_q, (void **)&req, 1);
348         if (ret != 1) {
349                 RTE_LOG(ERR, KNI, "Fail to put the muf back to resp_q\n");
350                 return -1; /* It is an error of can't putting the mbuf back */
351         }
352
353         return 0;
354 }
355
356 unsigned
357 rte_kni_tx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
358 {
359         unsigned ret = kni_fifo_put(kni->rx_q, (void **)mbufs, num);
360
361         /* Get mbufs from free_q and then free them */
362         kni_free_mbufs(kni);
363
364         return ret;
365 }
366
367 unsigned
368 rte_kni_rx_burst(struct rte_kni *kni, struct rte_mbuf **mbufs, unsigned num)
369 {
370         unsigned ret = kni_fifo_get(kni->tx_q, (void **)mbufs, num);
371
372         /* Allocate mbufs and then put them into alloc_q */
373         kni_allocate_mbufs(kni);
374
375         return ret;
376 }
377
378 static void
379 kni_free_mbufs(struct rte_kni *kni)
380 {
381         int i, ret;
382         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
383
384         ret = kni_fifo_get(kni->free_q, (void **)pkts, MAX_MBUF_BURST_NUM);
385         if (likely(ret > 0)) {
386                 for (i = 0; i < ret; i++)
387                         rte_pktmbuf_free(pkts[i]);
388         }
389 }
390
391 static void
392 kni_allocate_mbufs(struct rte_kni *kni)
393 {
394         int i, ret;
395         struct rte_mbuf *pkts[MAX_MBUF_BURST_NUM];
396
397         /* Check if pktmbuf pool has been configured */
398         if (kni->pktmbuf_pool == NULL) {
399                 RTE_LOG(ERR, KNI, "No valid mempool for allocating mbufs\n");
400                 return;
401         }
402
403         for (i = 0; i < MAX_MBUF_BURST_NUM; i++) {
404                 pkts[i] = rte_pktmbuf_alloc(kni->pktmbuf_pool);
405                 if (unlikely(pkts[i] == NULL)) {
406                         /* Out of memory */
407                         RTE_LOG(ERR, KNI, "Out of memory\n");
408                         break;
409                 }
410         }
411
412         /* No pkt mbuf alocated */
413         if (i <= 0)
414                 return;
415
416         ret = kni_fifo_put(kni->alloc_q, (void **)pkts, i);
417
418         /* Check if any mbufs not put into alloc_q, and then free them */
419         if (ret >= 0 && ret < i && ret < MAX_MBUF_BURST_NUM) {
420                 int j;
421
422                 for (j = ret; j < i; j++)
423                         rte_pktmbuf_free(pkts[j]);
424         }
425 }
426
427 /* It is deprecated and just for backward compatibility */
428 uint8_t
429 rte_kni_get_port_id(struct rte_kni *kni)
430 {
431         if (!kni)
432                 return ~0x0;
433
434         return kni->ops.port_id;
435 }
436
437 struct rte_kni *
438 rte_kni_get(const char *name)
439 {
440         struct rte_kni *kni;
441         const struct rte_memzone *mz;
442         char mz_name[RTE_MEMZONE_NAMESIZE];
443
444         if (!name || !name[0])
445                 return NULL;
446
447         rte_snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "KNI_INFO_%s", name);
448         mz = rte_memzone_lookup(mz_name);
449         if (!mz)
450                 return NULL;
451
452         kni = mz->addr;
453         if (!kni->in_use)
454                 return NULL;
455
456         return kni;
457 }
458
459 /*
460  * It is deprecated and just for backward compatibility.
461  */
462 struct rte_kni *
463 rte_kni_info_get(uint8_t port_id)
464 {
465         char name[RTE_MEMZONE_NAMESIZE];
466
467         if (port_id >= RTE_MAX_ETHPORTS)
468                 return NULL;
469
470         rte_snprintf(name, RTE_MEMZONE_NAMESIZE, "vEth%u", port_id);
471
472         return rte_kni_get(name);
473 }
474
475 static enum kni_ops_status
476 kni_check_request_register(struct rte_kni_ops *ops)
477 {
478         /* check if KNI request ops has been registered*/
479         if( NULL == ops )
480                 return KNI_REQ_NO_REGISTER;
481
482         if((NULL == ops->change_mtu) && (NULL == ops->config_network_if))
483                 return KNI_REQ_NO_REGISTER;
484
485         return KNI_REQ_REGISTERED;
486 }
487
488 int
489 rte_kni_register_handlers(struct rte_kni *kni,struct rte_kni_ops *ops)
490 {
491         enum kni_ops_status req_status;
492
493         if (NULL == ops) {
494                 RTE_LOG(ERR, KNI, "Invalid KNI request operation.\n");
495                 return -1;
496         }
497
498         if (NULL == kni) {
499                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
500                 return -1;
501         }
502
503         req_status = kni_check_request_register(&kni->ops);
504         if ( KNI_REQ_REGISTERED == req_status) {
505                 RTE_LOG(ERR, KNI, "The KNI request operation"
506                                         "has already registered.\n");
507                 return -1;
508         }
509
510         memcpy(&kni->ops, ops, sizeof(struct rte_kni_ops));
511         return 0;
512 }
513
514 int
515 rte_kni_unregister_handlers(struct rte_kni *kni)
516 {
517         if (NULL == kni) {
518                 RTE_LOG(ERR, KNI, "Invalid kni info.\n");
519                 return -1;
520         }
521
522         kni->ops.change_mtu = NULL;
523         kni->ops.config_network_if = NULL;
524         return 0;
525 }
526 void
527 rte_kni_close(void)
528 {
529         if (kni_fd < 0)
530                 return;
531
532         close(kni_fd);
533         kni_fd = -1;
534 }