Skip to content

Commit ffd956e

Browse files
can: introduce CAN midlayer private and allocate it automatically
This patch introduces the CAN midlayer private structure ("struct can_ml_priv") which should be used to hold protocol specific per device data structures. For now it's only member is "struct can_dev_rcv_lists". The CAN midlayer private is allocated via alloc_netdev()'s private and assigned to "struct net_device::ml_priv" during device creation. This is done transparently for CAN drivers using alloc_candev(). The slcan, vcan and vxcan drivers which are not using alloc_candev() have been adopted manually. The memory layout of the netdev_priv allocated via alloc_candev() will looke like this: +-------------------------+ | driver's priv | +-------------------------+ | struct can_ml_priv | +-------------------------+ | array of struct sk_buff | +-------------------------+ Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 3f15035 commit ffd956e

8 files changed

Lines changed: 96 additions & 23 deletions

File tree

‎drivers/net/can/dev.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/if_arp.h>
1313
#include <linux/workqueue.h>
1414
#include <linux/can.h>
15+
#include <linux/can/can-ml.h>
1516
#include <linux/can/dev.h>
1617
#include <linux/can/skb.h>
1718
#include <linux/can/netlink.h>
@@ -718,11 +719,24 @@ struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
718719
struct can_priv *priv;
719720
int size;
720721

722+
/* We put the driver's priv, the CAN mid layer priv and the
723+
* echo skb into the netdevice's priv. The memory layout for
724+
* the netdev_priv is like this:
725+
*
726+
* +-------------------------+
727+
* | driver's priv |
728+
* +-------------------------+
729+
* | struct can_ml_priv |
730+
* +-------------------------+
731+
* | array of struct sk_buff |
732+
* +-------------------------+
733+
*/
734+
735+
size = ALIGN(sizeof_priv, NETDEV_ALIGN) + sizeof(struct can_ml_priv);
736+
721737
if (echo_skb_max)
722-
size = ALIGN(sizeof_priv, sizeof(struct sk_buff *)) +
738+
size = ALIGN(size, sizeof(struct sk_buff *)) +
723739
echo_skb_max * sizeof(struct sk_buff *);
724-
else
725-
size = sizeof_priv;
726740

727741
dev = alloc_netdev_mqs(size, "can%d", NET_NAME_UNKNOWN, can_setup,
728742
txqs, rxqs);
@@ -735,7 +749,7 @@ struct net_device *alloc_candev_mqs(int sizeof_priv, unsigned int echo_skb_max,
735749
if (echo_skb_max) {
736750
priv->echo_skb_max = echo_skb_max;
737751
priv->echo_skb = (void *)priv +
738-
ALIGN(sizeof_priv, sizeof(struct sk_buff *));
752+
(size - echo_skb_max * sizeof(struct sk_buff *));
739753
}
740754

741755
priv->state = CAN_STATE_STOPPED;

‎drivers/net/can/slcan.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include <linux/workqueue.h>
5656
#include <linux/can.h>
5757
#include <linux/can/skb.h>
58+
#include <linux/can/can-ml.h>
5859

5960
MODULE_ALIAS_LDISC(N_SLCAN);
6061
MODULE_DESCRIPTION("serial line CAN interface");
@@ -514,6 +515,7 @@ static struct slcan *slc_alloc(void)
514515
char name[IFNAMSIZ];
515516
struct net_device *dev = NULL;
516517
struct slcan *sl;
518+
int size;
517519

518520
for (i = 0; i < maxdev; i++) {
519521
dev = slcan_devs[i];
@@ -527,7 +529,8 @@ static struct slcan *slc_alloc(void)
527529
return NULL;
528530

529531
sprintf(name, "slcan%d", i);
530-
dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, slc_setup);
532+
size = ALIGN(sizeof(*sl), NETDEV_ALIGN) + sizeof(struct can_ml_priv);
533+
dev = alloc_netdev(size, name, NET_NAME_UNKNOWN, slc_setup);
531534
if (!dev)
532535
return NULL;
533536

‎drivers/net/can/vcan.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <linux/if_arp.h>
4747
#include <linux/if_ether.h>
4848
#include <linux/can.h>
49+
#include <linux/can/can-ml.h>
4950
#include <linux/can/dev.h>
5051
#include <linux/can/skb.h>
5152
#include <linux/slab.h>
@@ -162,8 +163,9 @@ static void vcan_setup(struct net_device *dev)
162163
}
163164

164165
static struct rtnl_link_ops vcan_link_ops __read_mostly = {
165-
.kind = DRV_NAME,
166-
.setup = vcan_setup,
166+
.kind = DRV_NAME,
167+
.priv_size = sizeof(struct can_ml_priv),
168+
.setup = vcan_setup,
167169
};
168170

169171
static __init int vcan_init_module(void)

‎drivers/net/can/vxcan.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/can/dev.h>
1919
#include <linux/can/skb.h>
2020
#include <linux/can/vxcan.h>
21+
#include <linux/can/can-ml.h>
2122
#include <linux/slab.h>
2223
#include <net/rtnetlink.h>
2324

@@ -281,7 +282,7 @@ static struct net *vxcan_get_link_net(const struct net_device *dev)
281282

282283
static struct rtnl_link_ops vxcan_link_ops = {
283284
.kind = DRV_NAME,
284-
.priv_size = sizeof(struct vxcan_priv),
285+
.priv_size = ALIGN(sizeof(struct vxcan_priv), NETDEV_ALIGN) + sizeof(struct can_ml_priv),
285286
.setup = vxcan_setup,
286287
.newlink = vxcan_newlink,
287288
.dellink = vxcan_dellink,

‎include/linux/can/can-ml.h‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
2+
/* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
3+
* Copyright (c) 2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
4+
*
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* 3. Neither the name of Volkswagen nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* Alternatively, provided that this notice is retained in full, this
20+
* software may be distributed under the terms of the GNU General
21+
* Public License ("GPL") version 2, in which case the provisions of the
22+
* GPL apply INSTEAD OF those given above.
23+
*
24+
* The provided data structures and external interfaces from this code
25+
* are not restricted to be used by modules with a GPL compatible license.
26+
*
27+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38+
* DAMAGE.
39+
*
40+
*/
41+
42+
#ifndef CAN_ML_H
43+
#define CAN_ML_H
44+
45+
#include <linux/can.h>
46+
#include <linux/list.h>
47+
48+
#define CAN_SFF_RCV_ARRAY_SZ (1 << CAN_SFF_ID_BITS)
49+
#define CAN_EFF_RCV_HASH_BITS 10
50+
#define CAN_EFF_RCV_ARRAY_SZ (1 << CAN_EFF_RCV_HASH_BITS)
51+
52+
enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_MAX };
53+
54+
struct can_dev_rcv_lists {
55+
struct hlist_head rx[RX_MAX];
56+
struct hlist_head rx_sff[CAN_SFF_RCV_ARRAY_SZ];
57+
struct hlist_head rx_eff[CAN_EFF_RCV_ARRAY_SZ];
58+
int remove_on_zero_entries;
59+
int entries;
60+
};
61+
62+
struct can_ml_priv {
63+
struct can_dev_rcv_lists dev_rcv_lists;
64+
};
65+
66+
#endif /* CAN_ML_H */

‎net/can/af_can.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <linux/can.h>
5959
#include <linux/can/core.h>
6060
#include <linux/can/skb.h>
61+
#include <linux/can/can-ml.h>
6162
#include <linux/ratelimit.h>
6263
#include <net/net_namespace.h>
6364
#include <net/sock.h>

‎net/can/af_can.h‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,6 @@ struct receiver {
6060
struct rcu_head rcu;
6161
};
6262

63-
#define CAN_SFF_RCV_ARRAY_SZ (1 << CAN_SFF_ID_BITS)
64-
#define CAN_EFF_RCV_HASH_BITS 10
65-
#define CAN_EFF_RCV_ARRAY_SZ (1 << CAN_EFF_RCV_HASH_BITS)
66-
67-
enum { RX_ERR, RX_ALL, RX_FIL, RX_INV, RX_MAX };
68-
69-
/* per device receive filters linked at dev->ml_priv */
70-
struct can_dev_rcv_lists {
71-
struct hlist_head rx[RX_MAX];
72-
struct hlist_head rx_sff[CAN_SFF_RCV_ARRAY_SZ];
73-
struct hlist_head rx_eff[CAN_EFF_RCV_ARRAY_SZ];
74-
int remove_on_zero_entries;
75-
int entries;
76-
};
77-
7863
/* statistic structures */
7964

8065
/* can be reset e.g. by can_init_stats() */

‎net/can/proc.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <linux/list.h>
4646
#include <linux/rcupdate.h>
4747
#include <linux/if_arp.h>
48+
#include <linux/can/can-ml.h>
4849
#include <linux/can/core.h>
4950

5051
#include "af_can.h"

0 commit comments

Comments
 (0)