Skip to content

Commit cb5635a

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: complete initial namespace support
The statistics and its proc output was not implemented as per-net in the initial network namespace support by Mario Kicherer (8e8cda6). This patch adds the missing per-net statistics for the CAN subsystem. Signed-off-by: Oliver Hartkopp <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent f2e72f4 commit cb5635a

File tree

5 files changed

+121
-105
lines changed

5 files changed

+121
-105
lines changed

include/linux/can/core.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Authors: Oliver Hartkopp <[email protected]>
77
* Urs Thuermann <[email protected]>
8-
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
8+
* Copyright (c) 2002-2017 Volkswagen Group Electronic Research
99
* All rights reserved.
1010
*
1111
*/
@@ -17,7 +17,7 @@
1717
#include <linux/skbuff.h>
1818
#include <linux/netdevice.h>
1919

20-
#define CAN_VERSION "20120528"
20+
#define CAN_VERSION "20170425"
2121

2222
/* increment this number each time you change some user-space interface */
2323
#define CAN_ABI_VERSION "9"

include/net/netns/can.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <linux/spinlock.h>
99

1010
struct dev_rcv_lists;
11+
struct s_stats;
12+
struct s_pstats;
1113

1214
struct netns_can {
1315
#if IS_ENABLED(CONFIG_PROC_FS)
@@ -26,6 +28,9 @@ struct netns_can {
2628
/* receive filters subscribed for 'all' CAN devices */
2729
struct dev_rcv_lists *can_rx_alldev_list;
2830
spinlock_t can_rcvlists_lock;
31+
struct timer_list can_stattimer;/* timer for statistics update */
32+
struct s_stats *can_stats; /* packet statistics */
33+
struct s_pstats *can_pstats; /* receive list statistics */
2934
};
3035

3136
#endif /* __NETNS_CAN_H__ */

net/can/af_can.c

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* af_can.c - Protocol family CAN core module
33
* (used by different CAN protocol modules)
44
*
5-
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
5+
* Copyright (c) 2002-2017 Volkswagen Group Electronic Research
66
* All rights reserved.
77
*
88
* Redistribution and use in source and binary forms, with or without
@@ -81,10 +81,6 @@ static struct kmem_cache *rcv_cache __read_mostly;
8181
static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
8282
static DEFINE_MUTEX(proto_tab_lock);
8383

84-
struct timer_list can_stattimer; /* timer for statistics update */
85-
struct s_stats can_stats; /* packet statistics */
86-
struct s_pstats can_pstats; /* receive list statistics */
87-
8884
static atomic_t skbcounter = ATOMIC_INIT(0);
8985

9086
/*
@@ -221,6 +217,7 @@ int can_send(struct sk_buff *skb, int loop)
221217
{
222218
struct sk_buff *newskb = NULL;
223219
struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
220+
struct s_stats *can_stats = dev_net(skb->dev)->can.can_stats;
224221
int err = -EINVAL;
225222

226223
if (skb->len == CAN_MTU) {
@@ -309,8 +306,8 @@ int can_send(struct sk_buff *skb, int loop)
309306
netif_rx_ni(newskb);
310307

311308
/* update statistics */
312-
can_stats.tx_frames++;
313-
can_stats.tx_frames_delta++;
309+
can_stats->tx_frames++;
310+
can_stats->tx_frames_delta++;
314311

315312
return 0;
316313

@@ -468,6 +465,7 @@ int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,
468465
struct receiver *r;
469466
struct hlist_head *rl;
470467
struct dev_rcv_lists *d;
468+
struct s_pstats *can_pstats = net->can.can_pstats;
471469
int err = 0;
472470

473471
/* insert new receiver (dev,canid,mask) -> (func,data) */
@@ -499,9 +497,9 @@ int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id,
499497
hlist_add_head_rcu(&r->list, rl);
500498
d->entries++;
501499

502-
can_pstats.rcv_entries++;
503-
if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
504-
can_pstats.rcv_entries_max = can_pstats.rcv_entries;
500+
can_pstats->rcv_entries++;
501+
if (can_pstats->rcv_entries_max < can_pstats->rcv_entries)
502+
can_pstats->rcv_entries_max = can_pstats->rcv_entries;
505503
} else {
506504
kmem_cache_free(rcv_cache, r);
507505
err = -ENODEV;
@@ -543,6 +541,7 @@ void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,
543541
{
544542
struct receiver *r = NULL;
545543
struct hlist_head *rl;
544+
struct s_pstats *can_pstats = net->can.can_pstats;
546545
struct dev_rcv_lists *d;
547546

548547
if (dev && dev->type != ARPHRD_CAN)
@@ -589,8 +588,8 @@ void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,
589588
hlist_del_rcu(&r->list);
590589
d->entries--;
591590

592-
if (can_pstats.rcv_entries > 0)
593-
can_pstats.rcv_entries--;
591+
if (can_pstats->rcv_entries > 0)
592+
can_pstats->rcv_entries--;
594593

595594
/* remove device structure requested by NETDEV_UNREGISTER */
596595
if (d->remove_on_zero_entries && !d->entries) {
@@ -684,11 +683,13 @@ static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
684683
static void can_receive(struct sk_buff *skb, struct net_device *dev)
685684
{
686685
struct dev_rcv_lists *d;
686+
struct net *net = dev_net(dev);
687+
struct s_stats *can_stats = net->can.can_stats;
687688
int matches;
688689

689690
/* update statistics */
690-
can_stats.rx_frames++;
691-
can_stats.rx_frames_delta++;
691+
can_stats->rx_frames++;
692+
can_stats->rx_frames_delta++;
692693

693694
/* create non-zero unique skb identifier together with *skb */
694695
while (!(can_skb_prv(skb)->skbcnt))
@@ -697,10 +698,10 @@ static void can_receive(struct sk_buff *skb, struct net_device *dev)
697698
rcu_read_lock();
698699

699700
/* deliver the packet to sockets listening on all devices */
700-
matches = can_rcv_filter(dev_net(dev)->can.can_rx_alldev_list, skb);
701+
matches = can_rcv_filter(net->can.can_rx_alldev_list, skb);
701702

702703
/* find receive list for this device */
703-
d = find_dev_rcv_lists(dev_net(dev), dev);
704+
d = find_dev_rcv_lists(net, dev);
704705
if (d)
705706
matches += can_rcv_filter(d, skb);
706707

@@ -710,8 +711,8 @@ static void can_receive(struct sk_buff *skb, struct net_device *dev)
710711
consume_skb(skb);
711712

712713
if (matches > 0) {
713-
can_stats.matches++;
714-
can_stats.matches_delta++;
714+
can_stats->matches++;
715+
can_stats->matches_delta++;
715716
}
716717
}
717718

@@ -876,8 +877,20 @@ static int can_pernet_init(struct net *net)
876877
net->can.can_rx_alldev_list =
877878
kzalloc(sizeof(struct dev_rcv_lists), GFP_KERNEL);
878879

879-
if (IS_ENABLED(CONFIG_PROC_FS))
880+
net->can.can_stats = kzalloc(sizeof(struct s_stats), GFP_KERNEL);
881+
net->can.can_pstats = kzalloc(sizeof(struct s_pstats), GFP_KERNEL);
882+
883+
if (IS_ENABLED(CONFIG_PROC_FS)) {
884+
/* the statistics are updated every second (timer triggered) */
885+
if (stats_timer) {
886+
setup_timer(&net->can.can_stattimer, can_stat_update,
887+
(unsigned long)net);
888+
mod_timer(&net->can.can_stattimer,
889+
round_jiffies(jiffies + HZ));
890+
}
891+
net->can.can_stats->jiffies_init = jiffies;
880892
can_init_proc(net);
893+
}
881894

882895
return 0;
883896
}
@@ -886,8 +899,11 @@ static void can_pernet_exit(struct net *net)
886899
{
887900
struct net_device *dev;
888901

889-
if (IS_ENABLED(CONFIG_PROC_FS))
902+
if (IS_ENABLED(CONFIG_PROC_FS)) {
890903
can_remove_proc(net);
904+
if (stats_timer)
905+
del_timer_sync(&net->can.can_stattimer);
906+
}
891907

892908
/* remove created dev_rcv_lists from still registered CAN devices */
893909
rcu_read_lock();
@@ -903,6 +919,8 @@ static void can_pernet_exit(struct net *net)
903919
rcu_read_unlock();
904920

905921
kfree(net->can.can_rx_alldev_list);
922+
kfree(net->can.can_stats);
923+
kfree(net->can.can_pstats);
906924
}
907925

908926
/*
@@ -950,14 +968,6 @@ static __init int can_init(void)
950968
if (!rcv_cache)
951969
return -ENOMEM;
952970

953-
if (IS_ENABLED(CONFIG_PROC_FS)) {
954-
if (stats_timer) {
955-
/* the statistics are updated every second (timer triggered) */
956-
setup_timer(&can_stattimer, can_stat_update, 0);
957-
mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
958-
}
959-
}
960-
961971
register_pernet_subsys(&can_pernet_ops);
962972

963973
/* protocol register */
@@ -971,11 +981,6 @@ static __init int can_init(void)
971981

972982
static __exit void can_exit(void)
973983
{
974-
if (IS_ENABLED(CONFIG_PROC_FS)) {
975-
if (stats_timer)
976-
del_timer_sync(&can_stattimer);
977-
}
978-
979984
/* protocol unregister */
980985
dev_remove_pack(&canfd_packet);
981986
dev_remove_pack(&can_packet);

net/can/af_can.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,4 @@ void can_init_proc(struct net *net);
115115
void can_remove_proc(struct net *net);
116116
void can_stat_update(unsigned long data);
117117

118-
/* structures and variables from af_can.c needed in proc.c for reading */
119-
extern struct timer_list can_stattimer; /* timer for statistics update */
120-
extern struct s_stats can_stats; /* packet statistics */
121-
extern struct s_pstats can_pstats; /* receive list statistics */
122-
123118
#endif /* AF_CAN_H */

0 commit comments

Comments
 (0)