Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netdevsim: forward skbs from one connected port to another

Forward skbs sent from one netdevsim port to its connected netdevsim
port using dev_forward_skb, in a spirit similar to veth.

Add a tx_dropped variable to struct netdevsim, tracking the number of
skbs that could not be forwarded using dev_forward_skb().

The xmit() function accessing the peer ptr is protected by an RCU read
critical section. The rcu_read_lock() is functionally redundant as since
v5.0 all softirqs are implicitly RCU read critical sections; but it is
useful for human readers.

If another CPU is concurrently in nsim_destroy(), then it will first set
the peer ptr to NULL. This does not affect any existing readers that
dereferenced a non-NULL peer. Then, in unregister_netdevice(), there is
a synchronize_rcu() before the netdev is actually unregistered and
freed. This ensures that any readers i.e. xmit() that got a non-NULL
peer will complete before the netdev is freed.

Any readers after the RCU_INIT_POINTER() but before synchronize_rcu()
will dereference NULL, making it safe.

The codepath to nsim_destroy() and nsim_create() takes both the newly
added nsim_dev_list_lock and rtnl_lock. This makes it safe with
concurrent calls to linking two netdevsims together.

Signed-off-by: David Wei <dw@davidwei.uk>
Reviewed-by: Maciek Machnikowski <maciek@machnikowski.net>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

David Wei and committed by
David S. Miller
9eb95228 f532957d

+23 -5
+22 -5
drivers/net/netdevsim/netdev.c
··· 29 29 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) 30 30 { 31 31 struct netdevsim *ns = netdev_priv(dev); 32 + unsigned int len = skb->len; 33 + struct netdevsim *peer_ns; 32 34 35 + rcu_read_lock(); 33 36 if (!nsim_ipsec_tx(ns, skb)) 34 - goto out; 37 + goto out_drop_free; 35 38 39 + peer_ns = rcu_dereference(ns->peer); 40 + if (!peer_ns) 41 + goto out_drop_free; 42 + 43 + skb_tx_timestamp(skb); 44 + if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP)) 45 + goto out_drop_cnt; 46 + 47 + rcu_read_unlock(); 36 48 u64_stats_update_begin(&ns->syncp); 37 49 ns->tx_packets++; 38 - ns->tx_bytes += skb->len; 50 + ns->tx_bytes += len; 39 51 u64_stats_update_end(&ns->syncp); 52 + return NETDEV_TX_OK; 40 53 41 - out: 54 + out_drop_free: 42 55 dev_kfree_skb(skb); 43 - 56 + out_drop_cnt: 57 + rcu_read_unlock(); 58 + u64_stats_update_begin(&ns->syncp); 59 + ns->tx_dropped++; 60 + u64_stats_update_end(&ns->syncp); 44 61 return NETDEV_TX_OK; 45 62 } 46 63 ··· 87 70 start = u64_stats_fetch_begin(&ns->syncp); 88 71 stats->tx_bytes = ns->tx_bytes; 89 72 stats->tx_packets = ns->tx_packets; 73 + stats->tx_dropped = ns->tx_dropped; 90 74 } while (u64_stats_fetch_retry(&ns->syncp, start)); 91 75 } 92 76 ··· 320 302 eth_hw_addr_random(dev); 321 303 322 304 dev->tx_queue_len = 0; 323 - dev->flags |= IFF_NOARP; 324 305 dev->flags &= ~IFF_MULTICAST; 325 306 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | 326 307 IFF_NO_QUEUE;
+1
drivers/net/netdevsim/netdevsim.h
··· 98 98 99 99 u64 tx_packets; 100 100 u64 tx_bytes; 101 + u64 tx_dropped; 101 102 struct u64_stats_sync syncp; 102 103 103 104 struct nsim_bus_dev *nsim_bus_dev;