blob: 6612946167feb1d85f2a74a493b73ad94ab0ac20 [file] [log] [blame]
Jiri Pirko007f7902014-11-28 14:34:17 +01001/*
2 * include/net/switchdev.h - Switch device API
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02003 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
Scott Feldmanf8f21472015-03-09 13:59:09 -07004 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
Jiri Pirko007f7902014-11-28 14:34:17 +01005 *
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>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010015#include <linux/notifier.h>
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020016#include <linux/list.h>
Jiri Pirko850d0cb2015-10-14 19:40:51 +020017#include <net/ip_fib.h>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010018
Scott Feldman30943332015-05-10 09:47:48 -070019#define SWITCHDEV_F_NO_RECURSE BIT(0)
Scott Feldman464314e2015-10-08 19:23:18 -070020#define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1)
Jiri Pirko0bc05d52015-10-14 19:40:50 +020021#define SWITCHDEV_F_DEFER BIT(2)
Scott Feldman30943332015-05-10 09:47:48 -070022
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020023struct switchdev_trans_item {
24 struct list_head list;
25 void *data;
26 void (*destructor)(const void *data);
27};
28
29struct switchdev_trans {
30 struct list_head item_list;
Jiri Pirkof623ab72015-09-24 10:02:49 +020031 bool ph_prepare;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020032};
33
Jiri Pirko8bdb4272015-09-24 10:02:43 +020034static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
35{
Jiri Pirkof623ab72015-09-24 10:02:49 +020036 return trans && trans->ph_prepare;
Jiri Pirko8bdb4272015-09-24 10:02:43 +020037}
38
39static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
40{
Jiri Pirkof623ab72015-09-24 10:02:49 +020041 return trans && !trans->ph_prepare;
Jiri Pirko8bdb4272015-09-24 10:02:43 +020042}
43
Scott Feldman30943332015-05-10 09:47:48 -070044enum switchdev_attr_id {
Jiri Pirko1f868392015-10-01 11:03:42 +020045 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,
Scott Feldmanf55ac582015-10-08 19:23:17 -070049 SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
Scott Feldman30943332015-05-10 09:47:48 -070050};
51
52struct switchdev_attr {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010053 struct net_device *orig_dev;
Scott Feldman30943332015-05-10 09:47:48 -070054 enum switchdev_attr_id id;
Scott Feldman30943332015-05-10 09:47:48 -070055 u32 flags;
Scott Feldmanf8e20a92015-05-10 09:47:49 -070056 union {
57 struct netdev_phys_item_id ppid; /* PORT_PARENT_ID */
Scott Feldman35636062015-05-10 09:47:51 -070058 u8 stp_state; /* PORT_STP_STATE */
Scott Feldman6004c862015-05-10 09:47:55 -070059 unsigned long brport_flags; /* PORT_BRIDGE_FLAGS */
Scott Feldmanf55ac582015-10-08 19:23:17 -070060 u32 ageing_time; /* BRIDGE_AGEING_TIME */
Scott Feldman42275bd2015-05-13 11:16:50 -070061 } u;
Scott Feldman30943332015-05-10 09:47:48 -070062};
63
Scott Feldman491d0f12015-05-10 09:47:52 -070064enum switchdev_obj_id {
Jiri Pirko57d80832015-10-01 11:03:41 +020065 SWITCHDEV_OBJ_ID_UNDEFINED,
66 SWITCHDEV_OBJ_ID_PORT_VLAN,
67 SWITCHDEV_OBJ_ID_IPV4_FIB,
68 SWITCHDEV_OBJ_ID_PORT_FDB,
Scott Feldman491d0f12015-05-10 09:47:52 -070069};
70
Jiri Pirko648b4a92015-10-01 11:03:45 +020071struct switchdev_obj {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010072 struct net_device *orig_dev;
Jiri Pirko9e8f4a52015-10-01 11:03:46 +020073 enum switchdev_obj_id id;
Jiri Pirko4d429c52015-10-14 19:40:52 +020074 u32 flags;
Jiri Pirko648b4a92015-10-01 11:03:45 +020075};
76
Jiri Pirko57d80832015-10-01 11:03:41 +020077/* SWITCHDEV_OBJ_ID_PORT_VLAN */
Jiri Pirko8f24f302015-10-01 11:03:43 +020078struct switchdev_obj_port_vlan {
Jiri Pirko648b4a92015-10-01 11:03:45 +020079 struct switchdev_obj obj;
Vivien Didelot44bbcf52015-09-29 12:07:18 -040080 u16 flags;
81 u16 vid_begin;
82 u16 vid_end;
83};
84
Jiri Pirko648b4a92015-10-01 11:03:45 +020085#define SWITCHDEV_OBJ_PORT_VLAN(obj) \
86 container_of(obj, struct switchdev_obj_port_vlan, obj)
87
Jiri Pirko57d80832015-10-01 11:03:41 +020088/* SWITCHDEV_OBJ_ID_IPV4_FIB */
Vivien Didelot44bbcf52015-09-29 12:07:18 -040089struct switchdev_obj_ipv4_fib {
Jiri Pirko648b4a92015-10-01 11:03:45 +020090 struct switchdev_obj obj;
Vivien Didelot44bbcf52015-09-29 12:07:18 -040091 u32 dst;
92 int dst_len;
Jiri Pirko850d0cb2015-10-14 19:40:51 +020093 struct fib_info fi;
Vivien Didelot44bbcf52015-09-29 12:07:18 -040094 u8 tos;
95 u8 type;
96 u32 nlflags;
97 u32 tb_id;
98};
99
Jiri Pirko648b4a92015-10-01 11:03:45 +0200100#define SWITCHDEV_OBJ_IPV4_FIB(obj) \
101 container_of(obj, struct switchdev_obj_ipv4_fib, obj)
102
Jiri Pirko57d80832015-10-01 11:03:41 +0200103/* SWITCHDEV_OBJ_ID_PORT_FDB */
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200104struct switchdev_obj_port_fdb {
Jiri Pirko648b4a92015-10-01 11:03:45 +0200105 struct switchdev_obj obj;
Jiri Pirko850d0cb2015-10-14 19:40:51 +0200106 unsigned char addr[ETH_ALEN];
Vivien Didelot44bbcf52015-09-29 12:07:18 -0400107 u16 vid;
108 u16 ndm_state;
Scott Feldman491d0f12015-05-10 09:47:52 -0700109};
110
Jiri Pirko648b4a92015-10-01 11:03:45 +0200111#define SWITCHDEV_OBJ_PORT_FDB(obj) \
112 container_of(obj, struct switchdev_obj_port_fdb, obj)
113
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200114void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
115 void *data, void (*destructor)(void const *),
116 struct switchdev_trans_item *tritem);
117void *switchdev_trans_item_dequeue(struct switchdev_trans *trans);
118
Jiri Pirko648b4a92015-10-01 11:03:45 +0200119typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
120
Scott Feldman41706042015-03-15 21:07:14 -0700121/**
122 * struct switchdev_ops - switchdev operations
123 *
Scott Feldman30943332015-05-10 09:47:48 -0700124 * @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 *
Vivien Didelot44bbcf52015-09-29 12:07:18 -0400128 * @switchdev_port_obj_add: Add an object to port (see switchdev_obj_*).
Scott Feldman491d0f12015-05-10 09:47:52 -0700129 *
Vivien Didelot44bbcf52015-09-29 12:07:18 -0400130 * @switchdev_port_obj_del: Delete an object from port (see switchdev_obj_*).
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700131 *
Vivien Didelot44bbcf52015-09-29 12:07:18 -0400132 * @switchdev_port_obj_dump: Dump port objects (see switchdev_obj_*).
Scott Feldman41706042015-03-15 21:07:14 -0700133 */
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700134struct switchdev_ops {
Scott Feldman30943332015-05-10 09:47:48 -0700135 int (*switchdev_port_attr_get)(struct net_device *dev,
136 struct switchdev_attr *attr);
137 int (*switchdev_port_attr_set)(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200138 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200139 struct switchdev_trans *trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700140 int (*switchdev_port_obj_add)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200141 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200142 struct switchdev_trans *trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700143 int (*switchdev_port_obj_del)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200144 const struct switchdev_obj *obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700145 int (*switchdev_port_obj_dump)(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200146 struct switchdev_obj *obj,
147 switchdev_obj_dump_cb_t *cb);
Scott Feldman41706042015-03-15 21:07:14 -0700148};
149
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700150enum switchdev_notifier_type {
151 SWITCHDEV_FDB_ADD = 1,
152 SWITCHDEV_FDB_DEL,
Jiri Pirko3aeb6612015-01-15 23:49:37 +0100153};
154
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700155struct switchdev_notifier_info {
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100156 struct net_device *dev;
157};
158
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700159struct switchdev_notifier_fdb_info {
160 struct switchdev_notifier_info info; /* must be first */
Jiri Pirko3aeb6612015-01-15 23:49:37 +0100161 const unsigned char *addr;
162 u16 vid;
163};
164
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100165static inline struct net_device *
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700166switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100167{
168 return info->dev;
169}
Jiri Pirko007f7902014-11-28 14:34:17 +0100170
171#ifdef CONFIG_NET_SWITCHDEV
172
Jiri Pirko793f4012015-10-14 19:40:48 +0200173void switchdev_deferred_process(void);
Scott Feldman30943332015-05-10 09:47:48 -0700174int switchdev_port_attr_get(struct net_device *dev,
175 struct switchdev_attr *attr);
176int switchdev_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200177 const struct switchdev_attr *attr);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200178int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200179 const struct switchdev_obj *obj);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200180int switchdev_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200181 const struct switchdev_obj *obj);
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200182int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200183 switchdev_obj_dump_cb_t *cb);
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700184int register_switchdev_notifier(struct notifier_block *nb);
185int unregister_switchdev_notifier(struct notifier_block *nb);
186int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
187 struct switchdev_notifier_info *info);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700188int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
189 struct net_device *dev, u32 filter_mask,
190 int nlflags);
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700191int switchdev_port_bridge_setlink(struct net_device *dev,
192 struct nlmsghdr *nlh, u16 flags);
193int switchdev_port_bridge_dellink(struct net_device *dev,
194 struct nlmsghdr *nlh, u16 flags);
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700195int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
196 u8 tos, u8 type, u32 nlflags, u32 tb_id);
197int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
198 u8 tos, u8 type, u32 tb_id);
199void switchdev_fib_ipv4_abort(struct fib_info *fi);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700200int 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);
203int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
204 struct net_device *dev, const unsigned char *addr,
205 u16 vid);
206int 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);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -0700209void switchdev_port_fwd_mark_set(struct net_device *dev,
210 struct net_device *group_dev,
211 bool joining);
Scott Feldman5e8d9042015-03-05 21:21:15 -0800212
Jiri Pirko007f7902014-11-28 14:34:17 +0100213#else
214
Jiri Pirko793f4012015-10-14 19:40:48 +0200215static inline void switchdev_deferred_process(void)
216{
217}
218
Scott Feldman30943332015-05-10 09:47:48 -0700219static inline int switchdev_port_attr_get(struct net_device *dev,
220 struct switchdev_attr *attr)
221{
222 return -EOPNOTSUPP;
223}
224
225static inline int switchdev_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200226 const struct switchdev_attr *attr)
Scott Feldman30943332015-05-10 09:47:48 -0700227{
228 return -EOPNOTSUPP;
229}
230
Scott Feldman491d0f12015-05-10 09:47:52 -0700231static inline int switchdev_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200232 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700233{
234 return -EOPNOTSUPP;
235}
236
237static inline int switchdev_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200238 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700239{
240 return -EOPNOTSUPP;
241}
242
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700243static inline int switchdev_port_obj_dump(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200244 const struct switchdev_obj *obj,
245 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700246{
247 return -EOPNOTSUPP;
248}
249
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700250static inline int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100251{
252 return 0;
253}
254
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700255static inline int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100256{
257 return 0;
258}
259
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700260static inline int call_switchdev_notifiers(unsigned long val,
261 struct net_device *dev,
262 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100263{
264 return NOTIFY_DONE;
265}
266
Scott Feldman8793d0a2015-05-10 09:48:04 -0700267static 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
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700274static inline int switchdev_port_bridge_setlink(struct net_device *dev,
275 struct nlmsghdr *nlh,
276 u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800277{
278 return -EOPNOTSUPP;
279}
280
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700281static inline int switchdev_port_bridge_dellink(struct net_device *dev,
282 struct nlmsghdr *nlh,
283 u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800284{
285 return -EOPNOTSUPP;
286}
287
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700288static 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)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800292{
293 return 0;
294}
295
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700296static inline int switchdev_fib_ipv4_del(u32 dst, int dst_len,
297 struct fib_info *fi,
298 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -0800299{
300 return 0;
301}
302
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700303static inline void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -0800304{
305}
306
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700307static 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
315static 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
322static 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{
Dragos Tatulea24cb7052015-11-16 10:52:48 +0100328 return idx;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700329}
330
Scott Feldman1a3b2ec2015-07-18 18:24:50 -0700331static inline void switchdev_port_fwd_mark_set(struct net_device *dev,
332 struct net_device *group_dev,
333 bool joining)
334{
335}
336
Jiri Pirko007f7902014-11-28 14:34:17 +0100337#endif
338
339#endif /* _LINUX_SWITCHDEV_H_ */