Skip to contentSkip to conversation

em_canid: Ematch rule to match CAN frames according to their identifiers

PATCH Superseded Rostislav Lisovy 2 participants 4 messages Jun 28, 2012 – Jul 2, 2012

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 em_canid: Ematch rule to match CAN frames according to their identifiers

This ematch makes it possible to classify CAN frames (AF_CAN) according to their identifiers. This functionality can not be easily achieved with existing classifiers, such as u32, because CAN identifier is always stored in native endianness, whereas u32 expects Network byte order.
---
Changes from the last time:
* Integrated remarks received from the mailing list
* Array containing rules (rules_raw) is dynamically allocated
* When processing a rule during configuration, CAN_EFF_FLAG in mask determines if we care about the value of CAN_EFF_FLAG in an identifier -- i.e. when CAN_EFF_FLAG is set in the mask, the rule will be added as SFF or EFF only depending on CAN_EFF_FLAG value in the identifier. If CAN_EFF_FLAG is 0 in the mask, the rule will be added as both SFF and EFF.

---
 include/linux/can.h     |    3 +
 include/linux/pkt_cls.h |    5 +-
 net/sched/Kconfig       |   10 ++
 net/sched/Makefile      |    1 +
 net/sched/em_canid.c    |  247 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 264 insertions(+), 2 deletions(-)
 create mode 100644 net/sched/em_canid.c
include/linux/can.h +3 -0
index 9a19bcb..08d1610 100644
@@ -38,6 +38,9 @@
  */
 typedef __u32 canid_t;
 
+#define CAN_SFF_ID_BITS		11
+#define CAN_EFF_ID_BITS		29
+
 /*
  * Controller Area Network Error Frame Mask structure
  *
include/linux/pkt_cls.h +3 -2
index defbde2..7fbe6c2 100644
@@ -451,8 +451,9 @@ enum {
 #define	TCF_EM_U32		3
 #define	TCF_EM_META		4
 #define	TCF_EM_TEXT		5
-#define        TCF_EM_VLAN		6
-#define	TCF_EM_MAX		6
+#define	TCF_EM_VLAN		6
+#define        TCF_EM_CANID		7
+#define	TCF_EM_MAX		7
 
 enum {
 	TCF_EM_PROG_TC
net/sched/Kconfig +10 -0
index 75b58f8..68a4c38 100644
@@ -485,6 +485,16 @@ config NET_EMATCH_TEXT
 	  To compile this code as a module, choose M here: the
 	  module will be called em_text.
 
+config NET_EMATCH_CANID
+	tristate "CAN Identifier"
+	depends on NET_EMATCH && CAN
+	---help---
+          Say Y here if you want to be able to classify CAN frames based
+          on CAN Identifier.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called em_canid.
+
 config NET_CLS_ACT
 	bool "Actions"
 	---help---
net/sched/Makefile +1 -0
index 8cdf4e2..47f9262 100644
@@ -53,3 +53,4 @@ obj-$(CONFIG_NET_EMATCH_NBYTE)	+= em_nbyte.o
 obj-$(CONFIG_NET_EMATCH_U32)	+= em_u32.o
 obj-$(CONFIG_NET_EMATCH_META)	+= em_meta.o
 obj-$(CONFIG_NET_EMATCH_TEXT)	+= em_text.o
+obj-$(CONFIG_NET_EMATCH_CANID)	+= em_canid.o
net/sched/em_canid.c new file +247 -0

247 lines not on this page · raw message

Signature · 1 line
1.7.10
maintainer
#1 lore raw

Hello Rostislav,

looks really good now.

1. Your Signed-off-by: is missing.

2. One remark to a removed length check:

(..)

net/sched/em_canid.c line 133

Quoted 11 lines
net/sched/em_canid.c new file +11 -0
+static int em_canid_change(struct tcf_proto *tp, void *data, int len,
+			  struct tcf_ematch *m)
+{
+	struct can_filter *conf = data; /* Array with rules,
+					 * fixed size EM_CAN_RULES_SIZE
+					 */
+	struct canid_match *cm;
+	struct canid_match *cm_old = (struct canid_match *) m->data;
+	int i;
+	int rulescnt;
+

What about a zero length check here?

	if (!len)
		return -EINVAL;

???

net/sched/em_canid.c line 145

Quoted 12 lines
net/sched/em_canid.c new file +12 -0
+	if (len % sizeof(struct can_filter))
+		return -EINVAL;
+
+	if (len > sizeof(struct can_filter) * EM_CAN_RULES_MAX)
+		return -EINVAL;
+
+	rulescnt = len / sizeof(struct can_filter);
+
+	cm = kzalloc(sizeof(struct canid_match) + sizeof(struct can_filter) *
+		rulescnt, GFP_KERNEL);
+	if (!cm)
+		return -ENOMEM;

The length could alternatively be checked here too

http://lxr.linux.no/#linux+v3.4.4/net/sched/ematch.c#L235

if em->ops->datalen is set.

But here's no

.datalen = sizeof(struct can_filter),

defined, right?

net/sched/em_canid.c line 230

Quoted 9 lines
net/sched/em_canid.c new file +9 -0
+static struct tcf_ematch_ops em_canid_ops = {
+	.kind	  = TCF_EM_CANID,
+	.change	  = em_canid_change,
+	.match	  = em_canid_match,
+	.destroy  = em_canid_destroy,
+	.dump	  = em_canid_dump,
+	.owner	  = THIS_MODULE,
+	.link	  = LIST_HEAD_INIT(em_canid_ops.link)
+};

Regards,
Oliver

maintainer
#2 lore raw

One additional remark:

When applying your patch on Dave Millers net-next tree there are some offsets in the patch ...

Please send the next patch based on the net-next tree, as this would be the tree, it will be applied to. See URLs at:

http://git.kernel.org/?p=linux/kernel/git/davem/net-next.git

Tnx!

Oliver

Quoted 73 lines · Oliver Hartkopp

Hello Rostislav,

looks really good now.

1. Your Signed-off-by: is missing.

2. One remark to a removed length check:

(..)

net/sched/em_canid.c line 133

Quoted 11 lines
net/sched/em_canid.c new file +11 -0
+static int em_canid_change(struct tcf_proto *tp, void *data, int len,
+			  struct tcf_ematch *m)
+{
+	struct can_filter *conf = data; /* Array with rules,
+					 * fixed size EM_CAN_RULES_SIZE
+					 */
+	struct canid_match *cm;
+	struct canid_match *cm_old = (struct canid_match *) m->data;
+	int i;
+	int rulescnt;
+

What about a zero length check here?

	if (!len)
		return -EINVAL;

???

net/sched/em_canid.c line 145

Quoted 12 lines
net/sched/em_canid.c new file +12 -0
+	if (len % sizeof(struct can_filter))
+		return -EINVAL;
+
+	if (len > sizeof(struct can_filter) * EM_CAN_RULES_MAX)
+		return -EINVAL;
+
+	rulescnt = len / sizeof(struct can_filter);
+
+	cm = kzalloc(sizeof(struct canid_match) + sizeof(struct can_filter) *
+		rulescnt, GFP_KERNEL);
+	if (!cm)
+		return -ENOMEM;

The length could alternatively be checked here too

http://lxr.linux.no/#linux+v3.4.4/net/sched/ematch.c#L235

if em->ops->datalen is set.

But here's no

.datalen = sizeof(struct can_filter),

defined, right?

net/sched/em_canid.c line 230

Quoted 9 lines
net/sched/em_canid.c new file +9 -0
+static struct tcf_ematch_ops em_canid_ops = {
+	.kind	  = TCF_EM_CANID,
+	.change	  = em_canid_change,
+	.match	  = em_canid_match,
+	.destroy  = em_canid_destroy,
+	.dump	  = em_canid_dump,
+	.owner	  = THIS_MODULE,
+	.link	  = LIST_HEAD_INIT(em_canid_ops.link)
+};

Regards,
Oliver
--
To unsubscribe from this list: send the line "unsubscribe linux-can" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Oliver Hartkopp

Hello Oliver;

Quoted 11 lines · Oliver Hartkopp

What about a zero length check here?

	if (!len)
		return -EINVAL;

The length could alternatively be checked here too

http://lxr.linux.no/#linux+v3.4.4/net/sched/ematch.c#L235

Quoted 8 lines

if em->ops->datalen is set.

But here's no

.datalen = sizeof(struct can_filter),

defined, right?

The main reason I didn't define the tcf_ematch_ops.datalen field is because the documentation says it is "length of expected configuration data" (not "minimal").
For the sake of possible future changes of the built-in length checking, I will do the check by myself -- I will add the zero-length check (at least all checks will be at the same place).

Regards;
Rostislav