Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* drivers/net/ifb.c:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 	The purpose of this driver is to provide a device that allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 	for sharing of resources:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 	1) qdiscs/policies that are per device as opposed to system wide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 	ifb allows for a device which can be redirected to thus providing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 	an impression of sharing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 	2) Allows for queueing incoming traffic for shaping instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 	dropping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	The original concept is based on what is known as the IMQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	driver initially written by Martin Devera, later rewritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	by Patrick McHardy and then maintained by Andre Correa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	You need the tc action  mirror or redirect to feed this device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)        	packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)   	Authors:	Jamal Hadi Salim (2005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define TX_Q_LIMIT    32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) struct ifb_q_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct net_device	*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct tasklet_struct   ifb_tasklet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int			tasklet_pending;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int			txqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct sk_buff_head     rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	u64			rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	u64			rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct u64_stats_sync	rsync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct u64_stats_sync	tsync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u64			tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	u64			tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct sk_buff_head     tq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) } ____cacheline_aligned_in_smp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) struct ifb_dev_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct ifb_q_private *tx_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static netdev_tx_t ifb_xmit(struct sk_buff *skb, struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static int ifb_open(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static int ifb_close(struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void ifb_ri_tasklet(unsigned long _txp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct ifb_q_private *txp = (struct ifb_q_private *)_txp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct netdev_queue *txq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	txq = netdev_get_tx_queue(txp->dev, txp->txqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	skb = skb_peek(&txp->tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (!__netif_tx_trylock(txq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			goto resched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		skb_queue_splice_tail_init(&txp->rq, &txp->tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		__netif_tx_unlock(txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	while ((skb = __skb_dequeue(&txp->tq)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		skb->redirected = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #ifdef CONFIG_NET_CLS_ACT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		skb->tc_skip_classify = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		u64_stats_update_begin(&txp->tsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		txp->tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		txp->tx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		u64_stats_update_end(&txp->tsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		skb->dev = dev_get_by_index_rcu(dev_net(txp->dev), skb->skb_iif);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (!skb->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			txp->dev->stats.tx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (skb_queue_len(&txp->tq) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				goto resched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		skb->skb_iif = txp->dev->ifindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		if (!skb->from_ingress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			dev_queue_xmit(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			skb_pull_rcsum(skb, skb->mac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			netif_receive_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (__netif_tx_trylock(txq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		skb = skb_peek(&txp->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			txp->tasklet_pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			if (netif_tx_queue_stopped(txq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				netif_tx_wake_queue(txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			__netif_tx_unlock(txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			goto resched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		__netif_tx_unlock(txq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) resched:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		txp->tasklet_pending = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		tasklet_schedule(&txp->ifb_tasklet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void ifb_stats64(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			struct rtnl_link_stats64 *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct ifb_dev_private *dp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct ifb_q_private *txp = dp->tx_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	unsigned int start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	u64 packets, bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	for (i = 0; i < dev->num_tx_queues; i++,txp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			start = u64_stats_fetch_begin_irq(&txp->rsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			packets = txp->rx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			bytes = txp->rx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		} while (u64_stats_fetch_retry_irq(&txp->rsync, start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		stats->rx_packets += packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		stats->rx_bytes += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			start = u64_stats_fetch_begin_irq(&txp->tsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			packets = txp->tx_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			bytes = txp->tx_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		} while (u64_stats_fetch_retry_irq(&txp->tsync, start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		stats->tx_packets += packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		stats->tx_bytes += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	stats->rx_dropped = dev->stats.rx_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	stats->tx_dropped = dev->stats.tx_dropped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static int ifb_dev_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct ifb_dev_private *dp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	struct ifb_q_private *txp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	txp = kcalloc(dev->num_tx_queues, sizeof(*txp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (!txp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	dp->tx_private = txp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	for (i = 0; i < dev->num_tx_queues; i++,txp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		txp->txqnum = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		txp->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		__skb_queue_head_init(&txp->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		__skb_queue_head_init(&txp->tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		u64_stats_init(&txp->rsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		u64_stats_init(&txp->tsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		tasklet_init(&txp->ifb_tasklet, ifb_ri_tasklet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			     (unsigned long)txp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		netif_tx_start_queue(netdev_get_tx_queue(dev, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static const struct net_device_ops ifb_netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.ndo_open	= ifb_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.ndo_stop	= ifb_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.ndo_get_stats64 = ifb_stats64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	.ndo_start_xmit	= ifb_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	.ndo_validate_addr = eth_validate_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.ndo_init	= ifb_dev_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #define IFB_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG  | NETIF_F_FRAGLIST	| \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		      NETIF_F_TSO_ECN | NETIF_F_TSO | NETIF_F_TSO6	| \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		      NETIF_F_GSO_ENCAP_ALL 				| \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		      NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX		| \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		      NETIF_F_HW_VLAN_STAG_TX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void ifb_dev_free(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct ifb_dev_private *dp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct ifb_q_private *txp = dp->tx_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	for (i = 0; i < dev->num_tx_queues; i++,txp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		tasklet_kill(&txp->ifb_tasklet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		__skb_queue_purge(&txp->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		__skb_queue_purge(&txp->tq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	kfree(dp->tx_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static void ifb_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/* Initialize the device structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	dev->netdev_ops = &ifb_netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	/* Fill in device structure with ethernet-generic values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	ether_setup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	dev->tx_queue_len = TX_Q_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	dev->features |= IFB_FEATURES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	dev->hw_features |= dev->features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	dev->hw_enc_features |= dev->features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	dev->vlan_features |= IFB_FEATURES & ~(NETIF_F_HW_VLAN_CTAG_TX |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 					       NETIF_F_HW_VLAN_STAG_TX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	dev->flags |= IFF_NOARP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	dev->flags &= ~IFF_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	netif_keep_dst(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	eth_hw_addr_random(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	dev->needs_free_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	dev->priv_destructor = ifb_dev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	dev->min_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	dev->max_mtu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static netdev_tx_t ifb_xmit(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct ifb_dev_private *dp = netdev_priv(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct ifb_q_private *txp = dp->tx_private + skb_get_queue_mapping(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	u64_stats_update_begin(&txp->rsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	txp->rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	txp->rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	u64_stats_update_end(&txp->rsync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (!skb->redirected || !skb->skb_iif) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (skb_queue_len(&txp->rq) >= dev->tx_queue_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		netif_tx_stop_queue(netdev_get_tx_queue(dev, txp->txqnum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	__skb_queue_tail(&txp->rq, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (!txp->tasklet_pending) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		txp->tasklet_pending = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		tasklet_schedule(&txp->ifb_tasklet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	return NETDEV_TX_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static int ifb_close(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	netif_tx_stop_all_queues(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static int ifb_open(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	netif_tx_start_all_queues(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int ifb_validate(struct nlattr *tb[], struct nlattr *data[],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			struct netlink_ext_ack *extack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (tb[IFLA_ADDRESS]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			return -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static struct rtnl_link_ops ifb_link_ops __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.kind		= "ifb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	.priv_size	= sizeof(struct ifb_dev_private),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.setup		= ifb_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.validate	= ifb_validate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /* Number of ifb devices to be set up by this module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * Note that these legacy devices have one queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  * Prefer something like : ip link add ifb10 numtxqueues 8 type ifb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int numifbs = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) module_param(numifbs, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) MODULE_PARM_DESC(numifbs, "Number of ifb devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int __init ifb_init_one(int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct net_device *dev_ifb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	dev_ifb = alloc_netdev(sizeof(struct ifb_dev_private), "ifb%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			       NET_NAME_UNKNOWN, ifb_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (!dev_ifb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	dev_ifb->rtnl_link_ops = &ifb_link_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	err = register_netdevice(dev_ifb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	free_netdev(dev_ifb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int __init ifb_init_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	down_write(&pernet_ops_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	err = __rtnl_link_register(&ifb_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	for (i = 0; i < numifbs && !err; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		err = ifb_init_one(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		__rtnl_link_unregister(&ifb_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	up_write(&pernet_ops_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static void __exit ifb_cleanup_module(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	rtnl_link_unregister(&ifb_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) module_init(ifb_init_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) module_exit(ifb_cleanup_module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) MODULE_AUTHOR("Jamal Hadi Salim");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_ALIAS_RTNL_LINK("ifb");