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