Skip to content

Commit

Permalink
can: complete initial namespace support
Browse files Browse the repository at this point in the history
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 <socketcan@hartkopp.net>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
  • Loading branch information
Oliver Hartkopp authored and Marc Kleine-Budde committed Apr 25, 2017
1 parent f2e72f4 commit cb5635a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 105 deletions.
4 changes: 2 additions & 2 deletions include/linux/can/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* Authors: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
* Urs Thuermann <urs.thuermann@volkswagen.de>
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
* Copyright (c) 2002-2017 Volkswagen Group Electronic Research
* All rights reserved.
*
*/
Expand All @@ -17,7 +17,7 @@
#include <linux/skbuff.h>
#include <linux/netdevice.h>

#define CAN_VERSION "20120528"
#define CAN_VERSION "20170425"

/* increment this number each time you change some user-space interface */
#define CAN_ABI_VERSION "9"
Expand Down
5 changes: 5 additions & 0 deletions include/net/netns/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <linux/spinlock.h>

struct dev_rcv_lists;
struct s_stats;
struct s_pstats;

struct netns_can {
#if IS_ENABLED(CONFIG_PROC_FS)
Expand All @@ -26,6 +28,9 @@ struct netns_can {
/* receive filters subscribed for 'all' CAN devices */
struct dev_rcv_lists *can_rx_alldev_list;
spinlock_t can_rcvlists_lock;
struct timer_list can_stattimer;/* timer for statistics update */
struct s_stats *can_stats; /* packet statistics */
struct s_pstats *can_pstats; /* receive list statistics */
};

#endif /* __NETNS_CAN_H__ */
71 changes: 38 additions & 33 deletions net/can/af_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* af_can.c - Protocol family CAN core module
* (used by different CAN protocol modules)
*
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
* Copyright (c) 2002-2017 Volkswagen Group Electronic Research
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -81,10 +81,6 @@ static struct kmem_cache *rcv_cache __read_mostly;
static const struct can_proto *proto_tab[CAN_NPROTO] __read_mostly;
static DEFINE_MUTEX(proto_tab_lock);

struct timer_list can_stattimer; /* timer for statistics update */
struct s_stats can_stats; /* packet statistics */
struct s_pstats can_pstats; /* receive list statistics */

static atomic_t skbcounter = ATOMIC_INIT(0);

/*
Expand Down Expand Up @@ -221,6 +217,7 @@ int can_send(struct sk_buff *skb, int loop)
{
struct sk_buff *newskb = NULL;
struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
struct s_stats *can_stats = dev_net(skb->dev)->can.can_stats;
int err = -EINVAL;

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

/* update statistics */
can_stats.tx_frames++;
can_stats.tx_frames_delta++;
can_stats->tx_frames++;
can_stats->tx_frames_delta++;

return 0;

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

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

can_pstats.rcv_entries++;
if (can_pstats.rcv_entries_max < can_pstats.rcv_entries)
can_pstats.rcv_entries_max = can_pstats.rcv_entries;
can_pstats->rcv_entries++;
if (can_pstats->rcv_entries_max < can_pstats->rcv_entries)
can_pstats->rcv_entries_max = can_pstats->rcv_entries;
} else {
kmem_cache_free(rcv_cache, r);
err = -ENODEV;
Expand Down Expand Up @@ -543,6 +541,7 @@ void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id,
{
struct receiver *r = NULL;
struct hlist_head *rl;
struct s_pstats *can_pstats = net->can.can_pstats;
struct dev_rcv_lists *d;

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

if (can_pstats.rcv_entries > 0)
can_pstats.rcv_entries--;
if (can_pstats->rcv_entries > 0)
can_pstats->rcv_entries--;

/* remove device structure requested by NETDEV_UNREGISTER */
if (d->remove_on_zero_entries && !d->entries) {
Expand Down Expand Up @@ -684,11 +683,13 @@ static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb)
static void can_receive(struct sk_buff *skb, struct net_device *dev)
{
struct dev_rcv_lists *d;
struct net *net = dev_net(dev);
struct s_stats *can_stats = net->can.can_stats;
int matches;

/* update statistics */
can_stats.rx_frames++;
can_stats.rx_frames_delta++;
can_stats->rx_frames++;
can_stats->rx_frames_delta++;

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

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

/* find receive list for this device */
d = find_dev_rcv_lists(dev_net(dev), dev);
d = find_dev_rcv_lists(net, dev);
if (d)
matches += can_rcv_filter(d, skb);

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

if (matches > 0) {
can_stats.matches++;
can_stats.matches_delta++;
can_stats->matches++;
can_stats->matches_delta++;
}
}

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

if (IS_ENABLED(CONFIG_PROC_FS))
net->can.can_stats = kzalloc(sizeof(struct s_stats), GFP_KERNEL);
net->can.can_pstats = kzalloc(sizeof(struct s_pstats), GFP_KERNEL);

if (IS_ENABLED(CONFIG_PROC_FS)) {
/* the statistics are updated every second (timer triggered) */
if (stats_timer) {
setup_timer(&net->can.can_stattimer, can_stat_update,
(unsigned long)net);
mod_timer(&net->can.can_stattimer,
round_jiffies(jiffies + HZ));
}
net->can.can_stats->jiffies_init = jiffies;
can_init_proc(net);
}

return 0;
}
Expand All @@ -886,8 +899,11 @@ static void can_pernet_exit(struct net *net)
{
struct net_device *dev;

if (IS_ENABLED(CONFIG_PROC_FS))
if (IS_ENABLED(CONFIG_PROC_FS)) {
can_remove_proc(net);
if (stats_timer)
del_timer_sync(&net->can.can_stattimer);
}

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

kfree(net->can.can_rx_alldev_list);
kfree(net->can.can_stats);
kfree(net->can.can_pstats);
}

/*
Expand Down Expand Up @@ -950,14 +968,6 @@ static __init int can_init(void)
if (!rcv_cache)
return -ENOMEM;

if (IS_ENABLED(CONFIG_PROC_FS)) {
if (stats_timer) {
/* the statistics are updated every second (timer triggered) */
setup_timer(&can_stattimer, can_stat_update, 0);
mod_timer(&can_stattimer, round_jiffies(jiffies + HZ));
}
}

register_pernet_subsys(&can_pernet_ops);

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

static __exit void can_exit(void)
{
if (IS_ENABLED(CONFIG_PROC_FS)) {
if (stats_timer)
del_timer_sync(&can_stattimer);
}

/* protocol unregister */
dev_remove_pack(&canfd_packet);
dev_remove_pack(&can_packet);
Expand Down
5 changes: 0 additions & 5 deletions net/can/af_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,4 @@ void can_init_proc(struct net *net);
void can_remove_proc(struct net *net);
void can_stat_update(unsigned long data);

/* structures and variables from af_can.c needed in proc.c for reading */
extern struct timer_list can_stattimer; /* timer for statistics update */
extern struct s_stats can_stats; /* packet statistics */
extern struct s_pstats can_pstats; /* receive list statistics */

#endif /* AF_CAN_H */
Loading

0 comments on commit cb5635a

Please sign in to comment.