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