]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/net/switchdev.h
ip_tunnel: Move stats update to iptunnel_xmit()
[karo-tx-linux.git] / include / net / switchdev.h
1 /*
2  * include/net/switchdev.h - Switch device API
3  * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
4  * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #ifndef _LINUX_SWITCHDEV_H_
12 #define _LINUX_SWITCHDEV_H_
13
14 #include <linux/netdevice.h>
15 #include <linux/notifier.h>
16 #include <linux/list.h>
17 #include <net/ip_fib.h>
18
19 #define SWITCHDEV_F_NO_RECURSE          BIT(0)
20 #define SWITCHDEV_F_SKIP_EOPNOTSUPP     BIT(1)
21 #define SWITCHDEV_F_DEFER               BIT(2)
22
23 struct switchdev_trans_item {
24         struct list_head list;
25         void *data;
26         void (*destructor)(const void *data);
27 };
28
29 struct switchdev_trans {
30         struct list_head item_list;
31         bool ph_prepare;
32 };
33
34 static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
35 {
36         return trans && trans->ph_prepare;
37 }
38
39 static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
40 {
41         return trans && !trans->ph_prepare;
42 }
43
44 enum switchdev_attr_id {
45         SWITCHDEV_ATTR_ID_UNDEFINED,
46         SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
47         SWITCHDEV_ATTR_ID_PORT_STP_STATE,
48         SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
49         SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
50 };
51
52 struct switchdev_attr {
53         struct net_device *orig_dev;
54         enum switchdev_attr_id id;
55         u32 flags;
56         union {
57                 struct netdev_phys_item_id ppid;        /* PORT_PARENT_ID */
58                 u8 stp_state;                           /* PORT_STP_STATE */
59                 unsigned long brport_flags;             /* PORT_BRIDGE_FLAGS */
60                 u32 ageing_time;                        /* BRIDGE_AGEING_TIME */
61         } u;
62 };
63
64 enum switchdev_obj_id {
65         SWITCHDEV_OBJ_ID_UNDEFINED,
66         SWITCHDEV_OBJ_ID_PORT_VLAN,
67         SWITCHDEV_OBJ_ID_IPV4_FIB,
68         SWITCHDEV_OBJ_ID_PORT_FDB,
69 };
70
71 struct switchdev_obj {
72         struct net_device *orig_dev;
73         enum switchdev_obj_id id;
74         u32 flags;
75 };
76
77 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
78 struct switchdev_obj_port_vlan {
79         struct switchdev_obj obj;
80         u16 flags;
81         u16 vid_begin;
82         u16 vid_end;
83 };
84
85 #define SWITCHDEV_OBJ_PORT_VLAN(obj) \
86         container_of(obj, struct switchdev_obj_port_vlan, obj)
87
88 /* SWITCHDEV_OBJ_ID_IPV4_FIB */
89 struct switchdev_obj_ipv4_fib {
90         struct switchdev_obj obj;
91         u32 dst;
92         int dst_len;
93         struct fib_info fi;
94         u8 tos;
95         u8 type;
96         u32 nlflags;
97         u32 tb_id;
98 };
99
100 #define SWITCHDEV_OBJ_IPV4_FIB(obj) \
101         container_of(obj, struct switchdev_obj_ipv4_fib, obj)
102
103 /* SWITCHDEV_OBJ_ID_PORT_FDB */
104 struct switchdev_obj_port_fdb {
105         struct switchdev_obj obj;
106         unsigned char addr[ETH_ALEN];
107         u16 vid;
108         u16 ndm_state;
109 };
110
111 #define SWITCHDEV_OBJ_PORT_FDB(obj) \
112         container_of(obj, struct switchdev_obj_port_fdb, obj)
113
114 void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
115                                   void *data, void (*destructor)(void const *),
116                                   struct switchdev_trans_item *tritem);
117 void *switchdev_trans_item_dequeue(struct switchdev_trans *trans);
118
119 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
120
121 /**
122  * struct switchdev_ops - switchdev operations
123  *
124  * @switchdev_port_attr_get: Get a port attribute (see switchdev_attr).
125  *
126  * @switchdev_port_attr_set: Set a port attribute (see switchdev_attr).
127  *
128  * @switchdev_port_obj_add: Add an object to port (see switchdev_obj_*).
129  *
130  * @switchdev_port_obj_del: Delete an object from port (see switchdev_obj_*).
131  *
132  * @switchdev_port_obj_dump: Dump port objects (see switchdev_obj_*).
133  */
134 struct switchdev_ops {
135         int     (*switchdev_port_attr_get)(struct net_device *dev,
136                                            struct switchdev_attr *attr);
137         int     (*switchdev_port_attr_set)(struct net_device *dev,
138                                            const struct switchdev_attr *attr,
139                                            struct switchdev_trans *trans);
140         int     (*switchdev_port_obj_add)(struct net_device *dev,
141                                           const struct switchdev_obj *obj,
142                                           struct switchdev_trans *trans);
143         int     (*switchdev_port_obj_del)(struct net_device *dev,
144                                           const struct switchdev_obj *obj);
145         int     (*switchdev_port_obj_dump)(struct net_device *dev,
146                                            struct switchdev_obj *obj,
147                                            switchdev_obj_dump_cb_t *cb);
148 };
149
150 enum switchdev_notifier_type {
151         SWITCHDEV_FDB_ADD = 1,
152         SWITCHDEV_FDB_DEL,
153 };
154
155 struct switchdev_notifier_info {
156         struct net_device *dev;
157 };
158
159 struct switchdev_notifier_fdb_info {
160         struct switchdev_notifier_info info; /* must be first */
161         const unsigned char *addr;
162         u16 vid;
163 };
164
165 static inline struct net_device *
166 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
167 {
168         return info->dev;
169 }
170
171 #ifdef CONFIG_NET_SWITCHDEV
172
173 void switchdev_deferred_process(void);
174 int switchdev_port_attr_get(struct net_device *dev,
175                             struct switchdev_attr *attr);
176 int switchdev_port_attr_set(struct net_device *dev,
177                             const struct switchdev_attr *attr);
178 int switchdev_port_obj_add(struct net_device *dev,
179                            const struct switchdev_obj *obj);
180 int switchdev_port_obj_del(struct net_device *dev,
181                            const struct switchdev_obj *obj);
182 int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
183                             switchdev_obj_dump_cb_t *cb);
184 int register_switchdev_notifier(struct notifier_block *nb);
185 int unregister_switchdev_notifier(struct notifier_block *nb);
186 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
187                              struct switchdev_notifier_info *info);
188 int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
189                                   struct net_device *dev, u32 filter_mask,
190                                   int nlflags);
191 int switchdev_port_bridge_setlink(struct net_device *dev,
192                                   struct nlmsghdr *nlh, u16 flags);
193 int switchdev_port_bridge_dellink(struct net_device *dev,
194                                   struct nlmsghdr *nlh, u16 flags);
195 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
196                            u8 tos, u8 type, u32 nlflags, u32 tb_id);
197 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
198                            u8 tos, u8 type, u32 tb_id);
199 void switchdev_fib_ipv4_abort(struct fib_info *fi);
200 int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
201                            struct net_device *dev, const unsigned char *addr,
202                            u16 vid, u16 nlm_flags);
203 int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
204                            struct net_device *dev, const unsigned char *addr,
205                            u16 vid);
206 int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
207                             struct net_device *dev,
208                             struct net_device *filter_dev, int idx);
209 void switchdev_port_fwd_mark_set(struct net_device *dev,
210                                  struct net_device *group_dev,
211                                  bool joining);
212
213 #else
214
215 static inline void switchdev_deferred_process(void)
216 {
217 }
218
219 static inline int switchdev_port_attr_get(struct net_device *dev,
220                                           struct switchdev_attr *attr)
221 {
222         return -EOPNOTSUPP;
223 }
224
225 static inline int switchdev_port_attr_set(struct net_device *dev,
226                                           const struct switchdev_attr *attr)
227 {
228         return -EOPNOTSUPP;
229 }
230
231 static inline int switchdev_port_obj_add(struct net_device *dev,
232                                          const struct switchdev_obj *obj)
233 {
234         return -EOPNOTSUPP;
235 }
236
237 static inline int switchdev_port_obj_del(struct net_device *dev,
238                                          const struct switchdev_obj *obj)
239 {
240         return -EOPNOTSUPP;
241 }
242
243 static inline int switchdev_port_obj_dump(struct net_device *dev,
244                                           const struct switchdev_obj *obj,
245                                           switchdev_obj_dump_cb_t *cb)
246 {
247         return -EOPNOTSUPP;
248 }
249
250 static inline int register_switchdev_notifier(struct notifier_block *nb)
251 {
252         return 0;
253 }
254
255 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
256 {
257         return 0;
258 }
259
260 static inline int call_switchdev_notifiers(unsigned long val,
261                                            struct net_device *dev,
262                                            struct switchdev_notifier_info *info)
263 {
264         return NOTIFY_DONE;
265 }
266
267 static inline int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid,
268                                             u32 seq, struct net_device *dev,
269                                             u32 filter_mask, int nlflags)
270 {
271         return -EOPNOTSUPP;
272 }
273
274 static inline int switchdev_port_bridge_setlink(struct net_device *dev,
275                                                 struct nlmsghdr *nlh,
276                                                 u16 flags)
277 {
278         return -EOPNOTSUPP;
279 }
280
281 static inline int switchdev_port_bridge_dellink(struct net_device *dev,
282                                                 struct nlmsghdr *nlh,
283                                                 u16 flags)
284 {
285         return -EOPNOTSUPP;
286 }
287
288 static inline int switchdev_fib_ipv4_add(u32 dst, int dst_len,
289                                          struct fib_info *fi,
290                                          u8 tos, u8 type,
291                                          u32 nlflags, u32 tb_id)
292 {
293         return 0;
294 }
295
296 static inline int switchdev_fib_ipv4_del(u32 dst, int dst_len,
297                                          struct fib_info *fi,
298                                          u8 tos, u8 type, u32 tb_id)
299 {
300         return 0;
301 }
302
303 static inline void switchdev_fib_ipv4_abort(struct fib_info *fi)
304 {
305 }
306
307 static inline int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
308                                          struct net_device *dev,
309                                          const unsigned char *addr,
310                                          u16 vid, u16 nlm_flags)
311 {
312         return -EOPNOTSUPP;
313 }
314
315 static inline int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
316                                          struct net_device *dev,
317                                          const unsigned char *addr, u16 vid)
318 {
319         return -EOPNOTSUPP;
320 }
321
322 static inline int switchdev_port_fdb_dump(struct sk_buff *skb,
323                                           struct netlink_callback *cb,
324                                           struct net_device *dev,
325                                           struct net_device *filter_dev,
326                                           int idx)
327 {
328        return idx;
329 }
330
331 static inline void switchdev_port_fwd_mark_set(struct net_device *dev,
332                                                struct net_device *group_dev,
333                                                bool joining)
334 {
335 }
336
337 #endif
338
339 #endif /* _LINUX_SWITCHDEV_H_ */