kni: support async user request
[dpdk.git] / kernel / linux / kni / kni_net.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2010-2014 Intel Corporation.
4  */
5
6 /*
7  * This code is inspired from the book "Linux Device Drivers" by
8  * Alessandro Rubini and Jonathan Corbet, published by O'Reilly & Associates
9  */
10
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/version.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h> /* eth_type_trans */
16 #include <linux/ethtool.h>
17 #include <linux/skbuff.h>
18 #include <linux/kthread.h>
19 #include <linux/delay.h>
20 #include <linux/rtnetlink.h>
21
22 #include <rte_kni_common.h>
23 #include <kni_fifo.h>
24
25 #include "compat.h"
26 #include "kni_dev.h"
27
28 #define WD_TIMEOUT 5 /*jiffies */
29
30 #define KNI_WAIT_RESPONSE_TIMEOUT 300 /* 3 seconds */
31
32 /* typedef for rx function */
33 typedef void (*kni_net_rx_t)(struct kni_dev *kni);
34
35 static void kni_net_rx_normal(struct kni_dev *kni);
36
37 /* kni rx function pointer, with default to normal rx */
38 static kni_net_rx_t kni_net_rx_func = kni_net_rx_normal;
39
40 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
41 /* iova to kernel virtual address */
42 static inline void *
43 iova2kva(struct kni_dev *kni, void *iova)
44 {
45         return phys_to_virt(iova_to_phys(kni->usr_tsk, (unsigned long)iova));
46 }
47
48 static inline void *
49 iova2data_kva(struct kni_dev *kni, struct rte_kni_mbuf *m)
50 {
51         return phys_to_virt(iova_to_phys(kni->usr_tsk, m->buf_iova) +
52                             m->data_off);
53 }
54 #endif
55
56 /* physical address to kernel virtual address */
57 static void *
58 pa2kva(void *pa)
59 {
60         return phys_to_virt((unsigned long)pa);
61 }
62
63 /* physical address to virtual address */
64 static void *
65 pa2va(void *pa, struct rte_kni_mbuf *m)
66 {
67         void *va;
68
69         va = (void *)((unsigned long)pa +
70                         (unsigned long)m->buf_addr -
71                         (unsigned long)m->buf_iova);
72         return va;
73 }
74
75 /* mbuf data kernel virtual address from mbuf kernel virtual address */
76 static void *
77 kva2data_kva(struct rte_kni_mbuf *m)
78 {
79         return phys_to_virt(m->buf_iova + m->data_off);
80 }
81
82 static inline void *
83 get_kva(struct kni_dev *kni, void *pa)
84 {
85 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
86         if (kni->iova_mode == 1)
87                 return iova2kva(kni, pa);
88 #endif
89         return pa2kva(pa);
90 }
91
92 static inline void *
93 get_data_kva(struct kni_dev *kni, void *pkt_kva)
94 {
95 #ifdef HAVE_IOVA_TO_KVA_MAPPING_SUPPORT
96         if (kni->iova_mode == 1)
97                 return iova2data_kva(kni, pkt_kva);
98 #endif
99         return kva2data_kva(pkt_kva);
100 }
101
102 /*
103  * It can be called to process the request.
104  */
105 static int
106 kni_net_process_request(struct net_device *dev, struct rte_kni_request *req)
107 {
108         struct kni_dev *kni = netdev_priv(dev);
109         int ret = -1;
110         void *resp_va;
111         uint32_t num;
112         int ret_val;
113
114         ASSERT_RTNL();
115
116         mutex_lock(&kni->sync_lock);
117
118         /* Construct data */
119         memcpy(kni->sync_kva, req, sizeof(struct rte_kni_request));
120         num = kni_fifo_put(kni->req_q, &kni->sync_va, 1);
121         if (num < 1) {
122                 pr_err("Cannot send to req_q\n");
123                 ret = -EBUSY;
124                 goto fail;
125         }
126
127         /* No result available since request is handled
128          * asynchronously. set response to success.
129          */
130         if (req->async != 0) {
131                 req->result = 0;
132                 goto async;
133         }
134
135         ret_val = wait_event_interruptible_timeout(kni->wq,
136                         kni_fifo_count(kni->resp_q), 3 * HZ);
137         if (signal_pending(current) || ret_val <= 0) {
138                 ret = -ETIME;
139                 goto fail;
140         }
141         num = kni_fifo_get(kni->resp_q, (void **)&resp_va, 1);
142         if (num != 1 || resp_va != kni->sync_va) {
143                 /* This should never happen */
144                 pr_err("No data in resp_q\n");
145                 ret = -ENODATA;
146                 goto fail;
147         }
148
149         memcpy(req, kni->sync_kva, sizeof(struct rte_kni_request));
150 async:
151         ret = 0;
152
153 fail:
154         mutex_unlock(&kni->sync_lock);
155         return ret;
156 }
157
158 /*
159  * Open and close
160  */
161 static int
162 kni_net_open(struct net_device *dev)
163 {
164         int ret;
165         struct rte_kni_request req;
166
167         netif_start_queue(dev);
168         if (kni_dflt_carrier == 1)
169                 netif_carrier_on(dev);
170         else
171                 netif_carrier_off(dev);
172
173         memset(&req, 0, sizeof(req));
174         req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
175
176         /* Setting if_up to non-zero means up */
177         req.if_up = 1;
178         ret = kni_net_process_request(dev, &req);
179
180         return (ret == 0) ? req.result : ret;
181 }
182
183 static int
184 kni_net_release(struct net_device *dev)
185 {
186         int ret;
187         struct rte_kni_request req;
188
189         netif_stop_queue(dev); /* can't transmit any more */
190         netif_carrier_off(dev);
191
192         memset(&req, 0, sizeof(req));
193         req.req_id = RTE_KNI_REQ_CFG_NETWORK_IF;
194
195         /* Setting if_up to 0 means down */
196         req.if_up = 0;
197         ret = kni_net_process_request(dev, &req);
198
199         return (ret == 0) ? req.result : ret;
200 }
201
202 static void
203 kni_fifo_trans_pa2va(struct kni_dev *kni,
204         struct rte_kni_fifo *src_pa, struct rte_kni_fifo *dst_va)
205 {
206         uint32_t ret, i, num_dst, num_rx;
207         struct rte_kni_mbuf *kva, *prev_kva;
208         int nb_segs;
209         int kva_nb_segs;
210
211         do {
212                 num_dst = kni_fifo_free_count(dst_va);
213                 if (num_dst == 0)
214                         return;
215
216                 num_rx = min_t(uint32_t, num_dst, MBUF_BURST_SZ);
217
218                 num_rx = kni_fifo_get(src_pa, kni->pa, num_rx);
219                 if (num_rx == 0)
220                         return;
221
222                 for (i = 0; i < num_rx; i++) {
223                         kva = get_kva(kni, kni->pa[i]);
224                         kni->va[i] = pa2va(kni->pa[i], kva);
225
226                         kva_nb_segs = kva->nb_segs;
227                         for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
228                                 if (!kva->next)
229                                         break;
230
231                                 prev_kva = kva;
232                                 kva = pa2kva(kva->next);
233                                 /* Convert physical address to virtual address */
234                                 prev_kva->next = pa2va(prev_kva->next, kva);
235                         }
236                 }
237
238                 ret = kni_fifo_put(dst_va, kni->va, num_rx);
239                 if (ret != num_rx) {
240                         /* Failing should not happen */
241                         pr_err("Fail to enqueue entries into dst_va\n");
242                         return;
243                 }
244         } while (1);
245 }
246
247 /* Try to release mbufs when kni release */
248 void kni_net_release_fifo_phy(struct kni_dev *kni)
249 {
250         /* release rx_q first, because it can't release in userspace */
251         kni_fifo_trans_pa2va(kni, kni->rx_q, kni->free_q);
252         /* release alloc_q for speeding up kni release in userspace */
253         kni_fifo_trans_pa2va(kni, kni->alloc_q, kni->free_q);
254 }
255
256 /*
257  * Configuration changes (passed on by ifconfig)
258  */
259 static int
260 kni_net_config(struct net_device *dev, struct ifmap *map)
261 {
262         if (dev->flags & IFF_UP) /* can't act on a running interface */
263                 return -EBUSY;
264
265         /* ignore other fields */
266         return 0;
267 }
268
269 /*
270  * Transmit a packet (called by the kernel)
271  */
272 static int
273 kni_net_tx(struct sk_buff *skb, struct net_device *dev)
274 {
275         int len = 0;
276         uint32_t ret;
277         struct kni_dev *kni = netdev_priv(dev);
278         struct rte_kni_mbuf *pkt_kva = NULL;
279         void *pkt_pa = NULL;
280         void *pkt_va = NULL;
281
282         /* save the timestamp */
283 #ifdef HAVE_TRANS_START_HELPER
284         netif_trans_update(dev);
285 #else
286         dev->trans_start = jiffies;
287 #endif
288
289         /* Check if the length of skb is less than mbuf size */
290         if (skb->len > kni->mbuf_size)
291                 goto drop;
292
293         /**
294          * Check if it has at least one free entry in tx_q and
295          * one entry in alloc_q.
296          */
297         if (kni_fifo_free_count(kni->tx_q) == 0 ||
298                         kni_fifo_count(kni->alloc_q) == 0) {
299                 /**
300                  * If no free entry in tx_q or no entry in alloc_q,
301                  * drops skb and goes out.
302                  */
303                 goto drop;
304         }
305
306         /* dequeue a mbuf from alloc_q */
307         ret = kni_fifo_get(kni->alloc_q, &pkt_pa, 1);
308         if (likely(ret == 1)) {
309                 void *data_kva;
310
311                 pkt_kva = get_kva(kni, pkt_pa);
312                 data_kva = get_data_kva(kni, pkt_kva);
313                 pkt_va = pa2va(pkt_pa, pkt_kva);
314
315                 len = skb->len;
316                 memcpy(data_kva, skb->data, len);
317                 if (unlikely(len < ETH_ZLEN)) {
318                         memset(data_kva + len, 0, ETH_ZLEN - len);
319                         len = ETH_ZLEN;
320                 }
321                 pkt_kva->pkt_len = len;
322                 pkt_kva->data_len = len;
323
324                 /* enqueue mbuf into tx_q */
325                 ret = kni_fifo_put(kni->tx_q, &pkt_va, 1);
326                 if (unlikely(ret != 1)) {
327                         /* Failing should not happen */
328                         pr_err("Fail to enqueue mbuf into tx_q\n");
329                         goto drop;
330                 }
331         } else {
332                 /* Failing should not happen */
333                 pr_err("Fail to dequeue mbuf from alloc_q\n");
334                 goto drop;
335         }
336
337         /* Free skb and update statistics */
338         dev_kfree_skb(skb);
339         dev->stats.tx_bytes += len;
340         dev->stats.tx_packets++;
341
342         return NETDEV_TX_OK;
343
344 drop:
345         /* Free skb and update statistics */
346         dev_kfree_skb(skb);
347         dev->stats.tx_dropped++;
348
349         return NETDEV_TX_OK;
350 }
351
352 /*
353  * RX: normal working mode
354  */
355 static void
356 kni_net_rx_normal(struct kni_dev *kni)
357 {
358         uint32_t ret;
359         uint32_t len;
360         uint32_t i, num_rx, num_fq;
361         struct rte_kni_mbuf *kva, *prev_kva;
362         void *data_kva;
363         struct sk_buff *skb;
364         struct net_device *dev = kni->net_dev;
365
366         /* Get the number of free entries in free_q */
367         num_fq = kni_fifo_free_count(kni->free_q);
368         if (num_fq == 0) {
369                 /* No room on the free_q, bail out */
370                 return;
371         }
372
373         /* Calculate the number of entries to dequeue from rx_q */
374         num_rx = min_t(uint32_t, num_fq, MBUF_BURST_SZ);
375
376         /* Burst dequeue from rx_q */
377         num_rx = kni_fifo_get(kni->rx_q, kni->pa, num_rx);
378         if (num_rx == 0)
379                 return;
380
381         /* Transfer received packets to netif */
382         for (i = 0; i < num_rx; i++) {
383                 kva = get_kva(kni, kni->pa[i]);
384                 len = kva->pkt_len;
385                 data_kva = get_data_kva(kni, kva);
386                 kni->va[i] = pa2va(kni->pa[i], kva);
387
388                 skb = netdev_alloc_skb(dev, len);
389                 if (!skb) {
390                         /* Update statistics */
391                         dev->stats.rx_dropped++;
392                         continue;
393                 }
394
395                 if (kva->nb_segs == 1) {
396                         memcpy(skb_put(skb, len), data_kva, len);
397                 } else {
398                         int nb_segs;
399                         int kva_nb_segs = kva->nb_segs;
400
401                         for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
402                                 memcpy(skb_put(skb, kva->data_len),
403                                         data_kva, kva->data_len);
404
405                                 if (!kva->next)
406                                         break;
407
408                                 prev_kva = kva;
409                                 kva = pa2kva(kva->next);
410                                 data_kva = kva2data_kva(kva);
411                                 /* Convert physical address to virtual address */
412                                 prev_kva->next = pa2va(prev_kva->next, kva);
413                         }
414                 }
415
416                 skb->protocol = eth_type_trans(skb, dev);
417                 skb->ip_summed = CHECKSUM_UNNECESSARY;
418
419                 /* Call netif interface */
420                 netif_rx_ni(skb);
421
422                 /* Update statistics */
423                 dev->stats.rx_bytes += len;
424                 dev->stats.rx_packets++;
425         }
426
427         /* Burst enqueue mbufs into free_q */
428         ret = kni_fifo_put(kni->free_q, kni->va, num_rx);
429         if (ret != num_rx)
430                 /* Failing should not happen */
431                 pr_err("Fail to enqueue entries into free_q\n");
432 }
433
434 /*
435  * RX: loopback with enqueue/dequeue fifos.
436  */
437 static void
438 kni_net_rx_lo_fifo(struct kni_dev *kni)
439 {
440         uint32_t ret;
441         uint32_t len;
442         uint32_t i, num, num_rq, num_tq, num_aq, num_fq;
443         struct rte_kni_mbuf *kva, *next_kva;
444         void *data_kva;
445         struct rte_kni_mbuf *alloc_kva;
446         void *alloc_data_kva;
447         struct net_device *dev = kni->net_dev;
448
449         /* Get the number of entries in rx_q */
450         num_rq = kni_fifo_count(kni->rx_q);
451
452         /* Get the number of free entries in tx_q */
453         num_tq = kni_fifo_free_count(kni->tx_q);
454
455         /* Get the number of entries in alloc_q */
456         num_aq = kni_fifo_count(kni->alloc_q);
457
458         /* Get the number of free entries in free_q */
459         num_fq = kni_fifo_free_count(kni->free_q);
460
461         /* Calculate the number of entries to be dequeued from rx_q */
462         num = min(num_rq, num_tq);
463         num = min(num, num_aq);
464         num = min(num, num_fq);
465         num = min_t(uint32_t, num, MBUF_BURST_SZ);
466
467         /* Return if no entry to dequeue from rx_q */
468         if (num == 0)
469                 return;
470
471         /* Burst dequeue from rx_q */
472         ret = kni_fifo_get(kni->rx_q, kni->pa, num);
473         if (ret == 0)
474                 return; /* Failing should not happen */
475
476         /* Dequeue entries from alloc_q */
477         ret = kni_fifo_get(kni->alloc_q, kni->alloc_pa, num);
478         if (ret) {
479                 num = ret;
480                 /* Copy mbufs */
481                 for (i = 0; i < num; i++) {
482                         kva = get_kva(kni, kni->pa[i]);
483                         len = kva->data_len;
484                         data_kva = get_data_kva(kni, kva);
485                         kni->va[i] = pa2va(kni->pa[i], kva);
486
487                         while (kva->next) {
488                                 next_kva = pa2kva(kva->next);
489                                 /* Convert physical address to virtual address */
490                                 kva->next = pa2va(kva->next, next_kva);
491                                 kva = next_kva;
492                         }
493
494                         alloc_kva = get_kva(kni, kni->alloc_pa[i]);
495                         alloc_data_kva = get_data_kva(kni, alloc_kva);
496                         kni->alloc_va[i] = pa2va(kni->alloc_pa[i], alloc_kva);
497
498                         memcpy(alloc_data_kva, data_kva, len);
499                         alloc_kva->pkt_len = len;
500                         alloc_kva->data_len = len;
501
502                         dev->stats.tx_bytes += len;
503                         dev->stats.rx_bytes += len;
504                 }
505
506                 /* Burst enqueue mbufs into tx_q */
507                 ret = kni_fifo_put(kni->tx_q, kni->alloc_va, num);
508                 if (ret != num)
509                         /* Failing should not happen */
510                         pr_err("Fail to enqueue mbufs into tx_q\n");
511         }
512
513         /* Burst enqueue mbufs into free_q */
514         ret = kni_fifo_put(kni->free_q, kni->va, num);
515         if (ret != num)
516                 /* Failing should not happen */
517                 pr_err("Fail to enqueue mbufs into free_q\n");
518
519         /**
520          * Update statistic, and enqueue/dequeue failure is impossible,
521          * as all queues are checked at first.
522          */
523         dev->stats.tx_packets += num;
524         dev->stats.rx_packets += num;
525 }
526
527 /*
528  * RX: loopback with enqueue/dequeue fifos and sk buffer copies.
529  */
530 static void
531 kni_net_rx_lo_fifo_skb(struct kni_dev *kni)
532 {
533         uint32_t ret;
534         uint32_t len;
535         uint32_t i, num_rq, num_fq, num;
536         struct rte_kni_mbuf *kva, *prev_kva;
537         void *data_kva;
538         struct sk_buff *skb;
539         struct net_device *dev = kni->net_dev;
540
541         /* Get the number of entries in rx_q */
542         num_rq = kni_fifo_count(kni->rx_q);
543
544         /* Get the number of free entries in free_q */
545         num_fq = kni_fifo_free_count(kni->free_q);
546
547         /* Calculate the number of entries to dequeue from rx_q */
548         num = min(num_rq, num_fq);
549         num = min_t(uint32_t, num, MBUF_BURST_SZ);
550
551         /* Return if no entry to dequeue from rx_q */
552         if (num == 0)
553                 return;
554
555         /* Burst dequeue mbufs from rx_q */
556         ret = kni_fifo_get(kni->rx_q, kni->pa, num);
557         if (ret == 0)
558                 return;
559
560         /* Copy mbufs to sk buffer and then call tx interface */
561         for (i = 0; i < num; i++) {
562                 kva = get_kva(kni, kni->pa[i]);
563                 len = kva->pkt_len;
564                 data_kva = get_data_kva(kni, kva);
565                 kni->va[i] = pa2va(kni->pa[i], kva);
566
567                 skb = netdev_alloc_skb(dev, len);
568                 if (skb) {
569                         memcpy(skb_put(skb, len), data_kva, len);
570                         skb->ip_summed = CHECKSUM_UNNECESSARY;
571                         dev_kfree_skb(skb);
572                 }
573
574                 /* Simulate real usage, allocate/copy skb twice */
575                 skb = netdev_alloc_skb(dev, len);
576                 if (skb == NULL) {
577                         dev->stats.rx_dropped++;
578                         continue;
579                 }
580
581                 if (kva->nb_segs == 1) {
582                         memcpy(skb_put(skb, len), data_kva, len);
583                 } else {
584                         int nb_segs;
585                         int kva_nb_segs = kva->nb_segs;
586
587                         for (nb_segs = 0; nb_segs < kva_nb_segs; nb_segs++) {
588                                 memcpy(skb_put(skb, kva->data_len),
589                                         data_kva, kva->data_len);
590
591                                 if (!kva->next)
592                                         break;
593
594                                 prev_kva = kva;
595                                 kva = get_kva(kni, kva->next);
596                                 data_kva = get_data_kva(kni, kva);
597                                 /* Convert physical address to virtual address */
598                                 prev_kva->next = pa2va(prev_kva->next, kva);
599                         }
600                 }
601
602                 skb->ip_summed = CHECKSUM_UNNECESSARY;
603
604                 dev->stats.rx_bytes += len;
605                 dev->stats.rx_packets++;
606
607                 /* call tx interface */
608                 kni_net_tx(skb, dev);
609         }
610
611         /* enqueue all the mbufs from rx_q into free_q */
612         ret = kni_fifo_put(kni->free_q, kni->va, num);
613         if (ret != num)
614                 /* Failing should not happen */
615                 pr_err("Fail to enqueue mbufs into free_q\n");
616 }
617
618 /* rx interface */
619 void
620 kni_net_rx(struct kni_dev *kni)
621 {
622         /**
623          * It doesn't need to check if it is NULL pointer,
624          * as it has a default value
625          */
626         (*kni_net_rx_func)(kni);
627 }
628
629 /*
630  * Deal with a transmit timeout.
631  */
632 #ifdef HAVE_TX_TIMEOUT_TXQUEUE
633 static void
634 kni_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
635 #else
636 static void
637 kni_net_tx_timeout(struct net_device *dev)
638 #endif
639 {
640         pr_debug("Transmit timeout at %ld, latency %ld\n", jiffies,
641                         jiffies - dev_trans_start(dev));
642
643         dev->stats.tx_errors++;
644         netif_wake_queue(dev);
645 }
646
647 static int
648 kni_net_change_mtu(struct net_device *dev, int new_mtu)
649 {
650         int ret;
651         struct rte_kni_request req;
652
653         pr_debug("kni_net_change_mtu new mtu %d to be set\n", new_mtu);
654
655         memset(&req, 0, sizeof(req));
656         req.req_id = RTE_KNI_REQ_CHANGE_MTU;
657         req.new_mtu = new_mtu;
658         ret = kni_net_process_request(dev, &req);
659         if (ret == 0 && req.result == 0)
660                 dev->mtu = new_mtu;
661
662         return (ret == 0) ? req.result : ret;
663 }
664
665 static void
666 kni_net_change_rx_flags(struct net_device *netdev, int flags)
667 {
668         struct rte_kni_request req;
669
670         memset(&req, 0, sizeof(req));
671
672         if (flags & IFF_ALLMULTI) {
673                 req.req_id = RTE_KNI_REQ_CHANGE_ALLMULTI;
674
675                 if (netdev->flags & IFF_ALLMULTI)
676                         req.allmulti = 1;
677                 else
678                         req.allmulti = 0;
679         }
680
681         if (flags & IFF_PROMISC) {
682                 req.req_id = RTE_KNI_REQ_CHANGE_PROMISC;
683
684                 if (netdev->flags & IFF_PROMISC)
685                         req.promiscusity = 1;
686                 else
687                         req.promiscusity = 0;
688         }
689
690         kni_net_process_request(netdev, &req);
691 }
692
693 /*
694  * Checks if the user space application provided the resp message
695  */
696 void
697 kni_net_poll_resp(struct kni_dev *kni)
698 {
699         if (kni_fifo_count(kni->resp_q))
700                 wake_up_interruptible(&kni->wq);
701 }
702
703 /*
704  *  Fill the eth header
705  */
706 static int
707 kni_net_header(struct sk_buff *skb, struct net_device *dev,
708                 unsigned short type, const void *daddr,
709                 const void *saddr, uint32_t len)
710 {
711         struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
712
713         memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
714         memcpy(eth->h_dest,   daddr ? daddr : dev->dev_addr, dev->addr_len);
715         eth->h_proto = htons(type);
716
717         return dev->hard_header_len;
718 }
719
720 /*
721  * Re-fill the eth header
722  */
723 #ifdef HAVE_REBUILD_HEADER
724 static int
725 kni_net_rebuild_header(struct sk_buff *skb)
726 {
727         struct net_device *dev = skb->dev;
728         struct ethhdr *eth = (struct ethhdr *) skb->data;
729
730         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
731         memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
732
733         return 0;
734 }
735 #endif /* < 4.1.0  */
736
737 /**
738  * kni_net_set_mac - Change the Ethernet Address of the KNI NIC
739  * @netdev: network interface device structure
740  * @p: pointer to an address structure
741  *
742  * Returns 0 on success, negative on failure
743  **/
744 static int
745 kni_net_set_mac(struct net_device *netdev, void *p)
746 {
747         int ret;
748         struct rte_kni_request req;
749         struct sockaddr *addr = p;
750
751         memset(&req, 0, sizeof(req));
752         req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR;
753
754         if (!is_valid_ether_addr((unsigned char *)(addr->sa_data)))
755                 return -EADDRNOTAVAIL;
756
757         memcpy(req.mac_addr, addr->sa_data, netdev->addr_len);
758         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
759
760         ret = kni_net_process_request(netdev, &req);
761
762         return (ret == 0 ? req.result : ret);
763 }
764
765 #ifdef HAVE_CHANGE_CARRIER_CB
766 static int
767 kni_net_change_carrier(struct net_device *dev, bool new_carrier)
768 {
769         if (new_carrier)
770                 netif_carrier_on(dev);
771         else
772                 netif_carrier_off(dev);
773         return 0;
774 }
775 #endif
776
777 static const struct header_ops kni_net_header_ops = {
778         .create  = kni_net_header,
779         .parse   = eth_header_parse,
780 #ifdef HAVE_REBUILD_HEADER
781         .rebuild = kni_net_rebuild_header,
782 #endif /* < 4.1.0  */
783         .cache   = NULL,  /* disable caching */
784 };
785
786 static const struct net_device_ops kni_net_netdev_ops = {
787         .ndo_open = kni_net_open,
788         .ndo_stop = kni_net_release,
789         .ndo_set_config = kni_net_config,
790         .ndo_change_rx_flags = kni_net_change_rx_flags,
791         .ndo_start_xmit = kni_net_tx,
792         .ndo_change_mtu = kni_net_change_mtu,
793         .ndo_tx_timeout = kni_net_tx_timeout,
794         .ndo_set_mac_address = kni_net_set_mac,
795 #ifdef HAVE_CHANGE_CARRIER_CB
796         .ndo_change_carrier = kni_net_change_carrier,
797 #endif
798 };
799
800 static void kni_get_drvinfo(struct net_device *dev,
801                             struct ethtool_drvinfo *info)
802 {
803         strlcpy(info->version, KNI_VERSION, sizeof(info->version));
804         strlcpy(info->driver, "kni", sizeof(info->driver));
805 }
806
807 static const struct ethtool_ops kni_net_ethtool_ops = {
808         .get_drvinfo    = kni_get_drvinfo,
809         .get_link       = ethtool_op_get_link,
810 };
811
812 void
813 kni_net_init(struct net_device *dev)
814 {
815         struct kni_dev *kni = netdev_priv(dev);
816
817         init_waitqueue_head(&kni->wq);
818         mutex_init(&kni->sync_lock);
819
820         ether_setup(dev); /* assign some of the fields */
821         dev->netdev_ops      = &kni_net_netdev_ops;
822         dev->header_ops      = &kni_net_header_ops;
823         dev->ethtool_ops     = &kni_net_ethtool_ops;
824         dev->watchdog_timeo = WD_TIMEOUT;
825 }
826
827 void
828 kni_net_config_lo_mode(char *lo_str)
829 {
830         if (!lo_str) {
831                 pr_debug("loopback disabled");
832                 return;
833         }
834
835         if (!strcmp(lo_str, "lo_mode_none"))
836                 pr_debug("loopback disabled");
837         else if (!strcmp(lo_str, "lo_mode_fifo")) {
838                 pr_debug("loopback mode=lo_mode_fifo enabled");
839                 kni_net_rx_func = kni_net_rx_lo_fifo;
840         } else if (!strcmp(lo_str, "lo_mode_fifo_skb")) {
841                 pr_debug("loopback mode=lo_mode_fifo_skb enabled");
842                 kni_net_rx_func = kni_net_rx_lo_fifo_skb;
843         } else {
844                 pr_debug("Unknown loopback parameter, disabled");
845         }
846 }