blob: 4bc925374a4b00c608ebfe2f2bae105dd026bf57 [file] [log] [blame]
Xiaoping Fan45f4f2c2015-05-22 16:13:00 -07001/*
2 * sfe_backport.h
3 * Shortcut forwarding engine compatible header file.
4 *
Xiaoping Fane70da412016-02-26 16:47:57 -08005 * Copyright (c) 2014-2016 The Linux Foundation. All rights reserved.
Xiaoping Fana42c68b2015-08-07 18:00:39 -07006 * Permission to use, copy, modify, and/or distribute this software for
7 * any purpose with or without fee is hereby granted, provided that the
8 * above copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Xiaoping Fan45f4f2c2015-05-22 16:13:00 -070016 */
17
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080018#include <linux/version.h>
19
Xiaoping Fane70da412016-02-26 16:47:57 -080020#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
Xiaoping Fan8da13872016-04-04 18:51:56 -070021#define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
22static unsigned int FN_NAME(void *priv, \
23 struct sk_buff *SKB, \
24 const struct nf_hook_state *state)
Xiaoping Fane70da412016-02-26 16:47:57 -080025#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
Xiaoping Fan8da13872016-04-04 18:51:56 -070026#define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
27static unsigned int FN_NAME(const struct nf_hook_ops *OPS, \
28 struct sk_buff *SKB, \
29 const struct net_device *UNUSED, \
30 const struct net_device *OUT, \
31 int (*OKFN)(struct sk_buff *))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080032#else
Xiaoping Fan8da13872016-04-04 18:51:56 -070033#define sfe_define_post_routing_hook(FN_NAME, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
34static unsigned int FN_NAME(unsigned int HOOKNUM, \
35 struct sk_buff *SKB, \
36 const struct net_device *UNUSED, \
37 const struct net_device *OUT, \
38 int (*OKFN)(struct sk_buff *))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -080039#endif
40
Xiaoping Fan8da13872016-04-04 18:51:56 -070041#define sfe_cm_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
42 sfe_define_post_routing_hook(__sfe_cm_ipv4_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN)
43#define sfe_cm_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
44 sfe_define_post_routing_hook(__sfe_cm_ipv6_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN)
45#define fast_classifier_ipv4_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
46 sfe_define_post_routing_hook(__fast_classifier_ipv4_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN)
Xiaoping Fan9b6bb332016-04-05 19:21:26 -070047#define fast_classifier_ipv6_post_routing_hook(HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN) \
48 sfe_define_post_routing_hook(__fast_classifier_ipv6_post_routing_hook, HOOKNUM, OPS, SKB, UNUSED, OUT, OKFN)
Xiaoping Fan8da13872016-04-04 18:51:56 -070049
Xiaoping Fane70da412016-02-26 16:47:57 -080050#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
51#define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \
52 { \
53 .hook = fn, \
54 .pf = NFPROTO_IPV4, \
55 .hooknum = NF_INET_POST_ROUTING, \
56 .priority = NF_IP_PRI_NAT_SRC + 1, \
57 }
58#else
59#define SFE_IPV4_NF_POST_ROUTING_HOOK(fn) \
60 { \
61 .hook = fn, \
62 .owner = THIS_MODULE, \
63 .pf = NFPROTO_IPV4, \
64 .hooknum = NF_INET_POST_ROUTING, \
65 .priority = NF_IP_PRI_NAT_SRC + 1, \
66 }
67#endif
68
69#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
70#define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \
71 { \
72 .hook = fn, \
73 .pf = NFPROTO_IPV6, \
74 .hooknum = NF_INET_POST_ROUTING, \
75 .priority = NF_IP_PRI_NAT_SRC + 1, \
76 }
77#else
78#define SFE_IPV6_NF_POST_ROUTING_HOOK(fn) \
79 { \
80 .hook = fn, \
81 .owner = THIS_MODULE, \
82 .pf = NFPROTO_IPV6, \
83 .hooknum = NF_INET_POST_ROUTING, \
84 .priority = NF_IP6_PRI_NAT_SRC + 1, \
85 }
86#endif
87
88#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 3, 0))
89#define SFE_NF_CT_DEFAULT_ZONE (&nf_ct_zone_dflt)
90#else
91#define SFE_NF_CT_DEFAULT_ZONE NF_CT_DEFAULT_ZONE
92#endif
93
Xiaoping Fan79ed7292015-03-27 16:47:19 -070094/*
95 * sfe_dev_get_master
96 * get master of bridge port, and hold it
97 */
98static inline struct net_device *sfe_dev_get_master(struct net_device *dev)
99{
100 struct net_device *master;
Xiaoping Fane70da412016-02-26 16:47:57 -0800101#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700102 rcu_read_lock();
103 master = netdev_master_upper_dev_get_rcu(dev);
104 if (master)
105 dev_hold(master);
106
107 rcu_read_unlock();
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800108#else
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700109 master = dev->master;
110 if (master)
111 dev_hold(master);
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800112#endif
Xiaoping Fan79ed7292015-03-27 16:47:19 -0700113 return master;
114}
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800115
Xiaoping Fane70da412016-02-26 16:47:57 -0800116#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700117#define SFE_DEV_EVENT_PTR(PTR) netdev_notifier_info_to_dev(PTR)
118#else
119#define SFE_DEV_EVENT_PTR(PTR) (struct net_device *)(PTR)
120#endif
121
122/*
Xiaoping Fan8da13872016-04-04 18:51:56 -0700123 * declare function sfe_dev_event_cb_t
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700124 */
Xiaoping Fan8da13872016-04-04 18:51:56 -0700125typedef int (*sfe_dev_event_cb_t)(struct notifier_block *this, unsigned long event, void *ptr);
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700126
127/*
Xiaoping Fan8da13872016-04-04 18:51:56 -0700128 * sfe_propagate_dev_event
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700129 * propagate ip address event as network device event
130 */
Xiaoping Fan8da13872016-04-04 18:51:56 -0700131static inline int sfe_propagate_dev_event(sfe_dev_event_cb_t fn, struct notifier_block *this, unsigned long event, struct net_device *dev)
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700132{
Xiaoping Fane70da412016-02-26 16:47:57 -0800133#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700134 struct netdev_notifier_info info;
135
136 netdev_notifier_info_init(&info, dev);
Xiaoping Fan8da13872016-04-04 18:51:56 -0700137 return fn(this, event, &info);
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700138#else
Xiaoping Fan8da13872016-04-04 18:51:56 -0700139 return fn(this, event, dev);
Xiaoping Fan20f04cc2015-03-30 10:14:20 -0700140#endif
141}
142
Xiaoping Fane70da412016-02-26 16:47:57 -0800143#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800144#define SFE_NF_CONN_ACCT(NM) struct nf_conn_acct *NM
145#else
146#define SFE_NF_CONN_ACCT(NM) struct nf_conn_counter *NM
147#endif
148
Xiaoping Fane70da412016-02-26 16:47:57 -0800149#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
Xiaoping Fan59176422015-05-22 15:58:10 -0700150#define SFE_ACCT_COUNTER(NM) ((NM)->counter)
Xiaoping Fan3f1fe512014-11-05 12:14:57 -0800151#else
152#define SFE_ACCT_COUNTER(NM) (NM)
153#endif
Xiaoping Fan8da13872016-04-04 18:51:56 -0700154
155#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
156#define sfe_hash_for_each_possible(name, obj, node, member, key) \
157 hash_for_each_possible(name, obj, member, key)
158#else
159#define sfe_hash_for_each_possible(name, obj, node, member, key) \
160 hash_for_each_possible(name, obj, node, member, key)
161#endif
162
163#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
164#define sfe_hash_for_each(name, bkt, node, obj, member) \
165 hash_for_each(name, bkt, obj, member)
166#else
167#define sfe_hash_for_each(name, bkt, node, obj, member) \
168 hash_for_each(name, bkt, node, obj, member)
169#endif