datapath: Enforce mutual exclusion between bridge and brcompat_mod.
[sliver-openvswitch.git] / datapath / compat.h
1 /*
2  * Copyright (c) 2011, 2012 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #ifndef COMPAT_H
10 #define COMPAT_H 1
11
12 #include <linux/netlink.h>
13
14 #ifndef HAVE_NLA_NUL_STRING
15 static inline int CHECK_NUL_STRING(struct nlattr *attr, int maxlen)
16 {
17         char *s;
18         int len;
19         if (!attr)
20                 return 0;
21
22         len = nla_len(attr);
23         if (len >= maxlen)
24                 return -EINVAL;
25
26         s = nla_data(attr);
27         if (s[len - 1] != '\0')
28                 return -EINVAL;
29
30         return 0;
31 }
32 #else
33 static inline int CHECK_NUL_STRING(struct nlattr *attr, int maxlen)
34 {
35         return 0;
36 }
37 #endif  /* !HAVE_NLA_NUL_STRING */
38
39 /*
40  * Enforces mutual exclusion with the Linux bridge module, by declaring and
41  * exporting br_should_route_hook.  Because the bridge module also exports the
42  * same symbol, the module loader will refuse to load both modules at the same
43  * time (e.g. "bridge: exports duplicate symbol br_should_route_hook (owned by
44  * openvswitch_mod)").
45  *
46  * Before Linux 2.6.36, Open vSwitch cannot safely coexist with the Linux
47  * bridge module, so openvswitch_mod uses this macro in those versions.  In
48  * Linux 2.6.36 and later, Open vSwitch can coexist with the bridge module, but
49  * it makes no sense to load both bridge and brcompat_mod, so brcompat_mod uses
50  * this macro in those versions.
51  *
52  * The use of "typeof" here avoids the need to track changes in the type of
53  * br_should_route_hook over various kernel versions.
54  */
55 #define BRIDGE_MUTUAL_EXCLUSION                                 \
56         typeof(br_should_route_hook) br_should_route_hook;      \
57         EXPORT_SYMBOL(br_should_route_hook)
58
59 #endif /* compat.h */