Skip to content

Commit 15fc1f7

Browse files
Eric Dumazetdavem330
authored andcommitted
sit: percpu stats accounting
Maintain per_cpu tx_bytes, tx_packets, rx_bytes, rx_packets. Other seldom used fields are kept in netdev->stats structure, possibly unsafe. This is a preliminary work to support lockless transmit path, and correct RX stats, that are already unsafe. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3c97af9 commit 15fc1f7

1 file changed

Lines changed: 64 additions & 18 deletions

File tree

‎net/ipv6/sit.c‎

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@
6363
#define HASH_SIZE 16
6464
#define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
6565

66-
static void ipip6_tunnel_init(struct net_device *dev);
66+
static int ipip6_tunnel_init(struct net_device *dev);
6767
static void ipip6_tunnel_setup(struct net_device *dev);
68+
static void ipip6_dev_free(struct net_device *dev);
6869

6970
static int sit_net_id __read_mostly;
7071
struct sit_net {
@@ -84,6 +85,33 @@ struct sit_net {
8485
#define for_each_ip_tunnel_rcu(start) \
8586
for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
8687

88+
/* often modified stats are per cpu, other are shared (netdev->stats) */
89+
struct pcpu_tstats {
90+
unsigned long rx_packets;
91+
unsigned long rx_bytes;
92+
unsigned long tx_packets;
93+
unsigned long tx_bytes;
94+
};
95+
96+
static struct net_device_stats *ipip6_get_stats(struct net_device *dev)
97+
{
98+
struct pcpu_tstats sum = { 0 };
99+
int i;
100+
101+
for_each_possible_cpu(i) {
102+
const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
103+
104+
sum.rx_packets += tstats->rx_packets;
105+
sum.rx_bytes += tstats->rx_bytes;
106+
sum.tx_packets += tstats->tx_packets;
107+
sum.tx_bytes += tstats->tx_bytes;
108+
}
109+
dev->stats.rx_packets = sum.rx_packets;
110+
dev->stats.rx_bytes = sum.rx_bytes;
111+
dev->stats.tx_packets = sum.tx_packets;
112+
dev->stats.tx_bytes = sum.tx_bytes;
113+
return &dev->stats;
114+
}
87115
/*
88116
* Must be invoked with rcu_read_lock
89117
*/
@@ -214,7 +242,7 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
214242
if (parms->name[0])
215243
strlcpy(name, parms->name, IFNAMSIZ);
216244
else
217-
sprintf(name, "sit%%d");
245+
strcpy(name, "sit%d");
218246

219247
dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
220248
if (dev == NULL)
@@ -230,7 +258,8 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
230258
nt = netdev_priv(dev);
231259

232260
nt->parms = *parms;
233-
ipip6_tunnel_init(dev);
261+
if (ipip6_tunnel_init(dev) < 0)
262+
goto failed_free;
234263
ipip6_tunnel_clone_6rd(dev, sitn);
235264

236265
if (parms->i_flags & SIT_ISATAP)
@@ -245,7 +274,7 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
245274
return nt;
246275

247276
failed_free:
248-
free_netdev(dev);
277+
ipip6_dev_free(dev);
249278
failed:
250279
return NULL;
251280
}
@@ -546,6 +575,8 @@ static int ipip6_rcv(struct sk_buff *skb)
546575
tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
547576
iph->saddr, iph->daddr);
548577
if (tunnel != NULL) {
578+
struct pcpu_tstats *tstats;
579+
549580
secpath_reset(skb);
550581
skb->mac_header = skb->network_header;
551582
skb_reset_network_header(skb);
@@ -561,7 +592,11 @@ static int ipip6_rcv(struct sk_buff *skb)
561592
return 0;
562593
}
563594

564-
skb_tunnel_rx(skb, tunnel->dev);
595+
tstats = this_cpu_ptr(tunnel->dev->tstats);
596+
tstats->rx_packets++;
597+
tstats->rx_bytes += skb->len;
598+
599+
__skb_tunnel_rx(skb, tunnel->dev);
565600

566601
ipip6_ecn_decapsulate(iph, skb);
567602

@@ -626,14 +661,13 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
626661
struct net_device *dev)
627662
{
628663
struct ip_tunnel *tunnel = netdev_priv(dev);
629-
struct net_device_stats *stats = &dev->stats;
630-
struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
664+
struct pcpu_tstats *tstats;
631665
struct iphdr *tiph = &tunnel->parms.iph;
632666
struct ipv6hdr *iph6 = ipv6_hdr(skb);
633667
u8 tos = tunnel->parms.iph.tos;
634668
__be16 df = tiph->frag_off;
635669
struct rtable *rt; /* Route to the other host */
636-
struct net_device *tdev; /* Device to other host */
670+
struct net_device *tdev; /* Device to other host */
637671
struct iphdr *iph; /* Our new IP header */
638672
unsigned int max_headroom; /* The extra header space needed */
639673
__be32 dst = tiph->daddr;
@@ -704,28 +738,28 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
704738
.oif = tunnel->parms.link,
705739
.proto = IPPROTO_IPV6 };
706740
if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
707-
stats->tx_carrier_errors++;
741+
dev->stats.tx_carrier_errors++;
708742
goto tx_error_icmp;
709743
}
710744
}
711745
if (rt->rt_type != RTN_UNICAST) {
712746
ip_rt_put(rt);
713-
stats->tx_carrier_errors++;
747+
dev->stats.tx_carrier_errors++;
714748
goto tx_error_icmp;
715749
}
716750
tdev = rt->dst.dev;
717751

718752
if (tdev == dev) {
719753
ip_rt_put(rt);
720-
stats->collisions++;
754+
dev->stats.collisions++;
721755
goto tx_error;
722756
}
723757

724758
if (df) {
725759
mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
726760

727761
if (mtu < 68) {
728-
stats->collisions++;
762+
dev->stats.collisions++;
729763
ip_rt_put(rt);
730764
goto tx_error;
731765
}
@@ -764,7 +798,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
764798
struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
765799
if (!new_skb) {
766800
ip_rt_put(rt);
767-
txq->tx_dropped++;
801+
dev->stats.tx_dropped++;
768802
dev_kfree_skb(skb);
769803
return NETDEV_TX_OK;
770804
}
@@ -800,14 +834,14 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
800834
iph->ttl = iph6->hop_limit;
801835

802836
nf_reset(skb);
803-
804-
IPTUNNEL_XMIT();
837+
tstats = this_cpu_ptr(dev->tstats);
838+
__IPTUNNEL_XMIT(tstats, &dev->stats);
805839
return NETDEV_TX_OK;
806840

807841
tx_error_icmp:
808842
dst_link_failure(skb);
809843
tx_error:
810-
stats->tx_errors++;
844+
dev->stats.tx_errors++;
811845
dev_kfree_skb(skb);
812846
return NETDEV_TX_OK;
813847
}
@@ -1084,12 +1118,19 @@ static const struct net_device_ops ipip6_netdev_ops = {
10841118
.ndo_start_xmit = ipip6_tunnel_xmit,
10851119
.ndo_do_ioctl = ipip6_tunnel_ioctl,
10861120
.ndo_change_mtu = ipip6_tunnel_change_mtu,
1121+
.ndo_get_stats = ipip6_get_stats,
10871122
};
10881123

1124+
static void ipip6_dev_free(struct net_device *dev)
1125+
{
1126+
free_percpu(dev->tstats);
1127+
free_netdev(dev);
1128+
}
1129+
10891130
static void ipip6_tunnel_setup(struct net_device *dev)
10901131
{
10911132
dev->netdev_ops = &ipip6_netdev_ops;
1092-
dev->destructor = free_netdev;
1133+
dev->destructor = ipip6_dev_free;
10931134

10941135
dev->type = ARPHRD_SIT;
10951136
dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
@@ -1101,7 +1142,7 @@ static void ipip6_tunnel_setup(struct net_device *dev)
11011142
dev->features |= NETIF_F_NETNS_LOCAL;
11021143
}
11031144

1104-
static void ipip6_tunnel_init(struct net_device *dev)
1145+
static int ipip6_tunnel_init(struct net_device *dev)
11051146
{
11061147
struct ip_tunnel *tunnel = netdev_priv(dev);
11071148

@@ -1112,6 +1153,11 @@ static void ipip6_tunnel_init(struct net_device *dev)
11121153
memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
11131154

11141155
ipip6_tunnel_bind_dev(dev);
1156+
dev->tstats = alloc_percpu(struct pcpu_tstats);
1157+
if (!dev->tstats)
1158+
return -ENOMEM;
1159+
1160+
return 0;
11151161
}
11161162

11171163
static void __net_init ipip6_fb_tunnel_init(struct net_device *dev)

0 commit comments

Comments
 (0)