1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * include/net/switchdev.h - Switch device API 4 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us> 5 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com> 6 */ 7 #ifndef _LINUX_SWITCHDEV_H_ 8 #define _LINUX_SWITCHDEV_H_ 9 10 #include <linux/netdevice.h> 11 #include <linux/notifier.h> 12 #include <linux/list.h> 13 #include <net/ip_fib.h> 14 15 #define SWITCHDEV_F_NO_RECURSE BIT(0) 16 #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1) 17 #define SWITCHDEV_F_DEFER BIT(2) 18 19 struct switchdev_trans { 20 bool ph_prepare; 21 }; 22 23 static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans) 24 { 25 return trans && trans->ph_prepare; 26 } 27 28 static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans) 29 { 30 return trans && !trans->ph_prepare; 31 } 32 33 enum switchdev_attr_id { 34 SWITCHDEV_ATTR_ID_UNDEFINED, 35 SWITCHDEV_ATTR_ID_PORT_STP_STATE, 36 SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, 37 SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS, 38 SWITCHDEV_ATTR_ID_PORT_MROUTER, 39 SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME, 40 SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, 41 SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 42 SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 43 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 44 SWITCHDEV_ATTR_ID_MRP_PORT_STATE, 45 SWITCHDEV_ATTR_ID_MRP_PORT_ROLE, 46 #endif 47 }; 48 49 struct switchdev_attr { 50 struct net_device *orig_dev; 51 enum switchdev_attr_id id; 52 u32 flags; 53 void *complete_priv; 54 void (*complete)(struct net_device *dev, int err, void *priv); 55 union { 56 u8 stp_state; /* PORT_STP_STATE */ 57 unsigned long brport_flags; /* PORT_{PRE}_BRIDGE_FLAGS */ 58 bool mrouter; /* PORT_MROUTER */ 59 clock_t ageing_time; /* BRIDGE_AGEING_TIME */ 60 bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */ 61 bool mc_disabled; /* MC_DISABLED */ 62 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 63 u8 mrp_port_state; /* MRP_PORT_STATE */ 64 u8 mrp_port_role; /* MRP_PORT_ROLE */ 65 u8 mrp_ring_state; /* MRP_RING_STATE */ 66 #endif 67 } u; 68 }; 69 70 enum switchdev_obj_id { 71 SWITCHDEV_OBJ_ID_UNDEFINED, 72 SWITCHDEV_OBJ_ID_PORT_VLAN, 73 SWITCHDEV_OBJ_ID_PORT_MDB, 74 SWITCHDEV_OBJ_ID_HOST_MDB, 75 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 76 SWITCHDEV_OBJ_ID_MRP, 77 SWITCHDEV_OBJ_ID_RING_TEST_MRP, 78 SWITCHDEV_OBJ_ID_RING_ROLE_MRP, 79 SWITCHDEV_OBJ_ID_RING_STATE_MRP, 80 #endif 81 }; 82 83 struct switchdev_obj { 84 struct net_device *orig_dev; 85 enum switchdev_obj_id id; 86 u32 flags; 87 void *complete_priv; 88 void (*complete)(struct net_device *dev, int err, void *priv); 89 }; 90 91 /* SWITCHDEV_OBJ_ID_PORT_VLAN */ 92 struct switchdev_obj_port_vlan { 93 struct switchdev_obj obj; 94 u16 flags; 95 u16 vid_begin; 96 u16 vid_end; 97 }; 98 99 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \ 100 container_of((OBJ), struct switchdev_obj_port_vlan, obj) 101 102 /* SWITCHDEV_OBJ_ID_PORT_MDB */ 103 struct switchdev_obj_port_mdb { 104 struct switchdev_obj obj; 105 unsigned char addr[ETH_ALEN]; 106 u16 vid; 107 }; 108 109 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \ 110 container_of((OBJ), struct switchdev_obj_port_mdb, obj) 111 112 113 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 114 /* SWITCHDEV_OBJ_ID_MRP */ 115 struct switchdev_obj_mrp { 116 struct switchdev_obj obj; 117 struct net_device *p_port; 118 struct net_device *s_port; 119 u32 ring_id; 120 }; 121 122 #define SWITCHDEV_OBJ_MRP(OBJ) \ 123 container_of((OBJ), struct switchdev_obj_mrp, obj) 124 125 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */ 126 struct switchdev_obj_ring_test_mrp { 127 struct switchdev_obj obj; 128 /* The value is in us and a value of 0 represents to stop */ 129 u32 interval; 130 u8 max_miss; 131 u32 ring_id; 132 u32 period; 133 }; 134 135 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \ 136 container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj) 137 138 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */ 139 struct switchdev_obj_ring_role_mrp { 140 struct switchdev_obj obj; 141 u8 ring_role; 142 u32 ring_id; 143 }; 144 145 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \ 146 container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj) 147 148 struct switchdev_obj_ring_state_mrp { 149 struct switchdev_obj obj; 150 u8 ring_state; 151 u32 ring_id; 152 }; 153 154 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \ 155 container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj) 156 157 #endif 158 159 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj); 160 161 enum switchdev_notifier_type { 162 SWITCHDEV_FDB_ADD_TO_BRIDGE = 1, 163 SWITCHDEV_FDB_DEL_TO_BRIDGE, 164 SWITCHDEV_FDB_ADD_TO_DEVICE, 165 SWITCHDEV_FDB_DEL_TO_DEVICE, 166 SWITCHDEV_FDB_OFFLOADED, 167 168 SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */ 169 SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */ 170 SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */ 171 172 SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE, 173 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE, 174 SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE, 175 SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE, 176 SWITCHDEV_VXLAN_FDB_OFFLOADED, 177 }; 178 179 struct switchdev_notifier_info { 180 struct net_device *dev; 181 struct netlink_ext_ack *extack; 182 }; 183 184 struct switchdev_notifier_fdb_info { 185 struct switchdev_notifier_info info; /* must be first */ 186 const unsigned char *addr; 187 u16 vid; 188 u8 added_by_user:1, 189 offloaded:1; 190 }; 191 192 struct switchdev_notifier_port_obj_info { 193 struct switchdev_notifier_info info; /* must be first */ 194 const struct switchdev_obj *obj; 195 struct switchdev_trans *trans; 196 bool handled; 197 }; 198 199 struct switchdev_notifier_port_attr_info { 200 struct switchdev_notifier_info info; /* must be first */ 201 const struct switchdev_attr *attr; 202 struct switchdev_trans *trans; 203 bool handled; 204 }; 205 206 static inline struct net_device * 207 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info) 208 { 209 return info->dev; 210 } 211 212 static inline struct netlink_ext_ack * 213 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info) 214 { 215 return info->extack; 216 } 217 218 #ifdef CONFIG_NET_SWITCHDEV 219 220 void switchdev_deferred_process(void); 221 int switchdev_port_attr_set(struct net_device *dev, 222 const struct switchdev_attr *attr); 223 int switchdev_port_obj_add(struct net_device *dev, 224 const struct switchdev_obj *obj, 225 struct netlink_ext_ack *extack); 226 int switchdev_port_obj_del(struct net_device *dev, 227 const struct switchdev_obj *obj); 228 229 int register_switchdev_notifier(struct notifier_block *nb); 230 int unregister_switchdev_notifier(struct notifier_block *nb); 231 int call_switchdev_notifiers(unsigned long val, struct net_device *dev, 232 struct switchdev_notifier_info *info, 233 struct netlink_ext_ack *extack); 234 235 int register_switchdev_blocking_notifier(struct notifier_block *nb); 236 int unregister_switchdev_blocking_notifier(struct notifier_block *nb); 237 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, 238 struct switchdev_notifier_info *info, 239 struct netlink_ext_ack *extack); 240 241 void switchdev_port_fwd_mark_set(struct net_device *dev, 242 struct net_device *group_dev, 243 bool joining); 244 245 int switchdev_handle_port_obj_add(struct net_device *dev, 246 struct switchdev_notifier_port_obj_info *port_obj_info, 247 bool (*check_cb)(const struct net_device *dev), 248 int (*add_cb)(struct net_device *dev, 249 const struct switchdev_obj *obj, 250 struct switchdev_trans *trans, 251 struct netlink_ext_ack *extack)); 252 int switchdev_handle_port_obj_del(struct net_device *dev, 253 struct switchdev_notifier_port_obj_info *port_obj_info, 254 bool (*check_cb)(const struct net_device *dev), 255 int (*del_cb)(struct net_device *dev, 256 const struct switchdev_obj *obj)); 257 258 int switchdev_handle_port_attr_set(struct net_device *dev, 259 struct switchdev_notifier_port_attr_info *port_attr_info, 260 bool (*check_cb)(const struct net_device *dev), 261 int (*set_cb)(struct net_device *dev, 262 const struct switchdev_attr *attr, 263 struct switchdev_trans *trans)); 264 #else 265 266 static inline void switchdev_deferred_process(void) 267 { 268 } 269 270 static inline int switchdev_port_attr_set(struct net_device *dev, 271 const struct switchdev_attr *attr) 272 { 273 return -EOPNOTSUPP; 274 } 275 276 static inline int switchdev_port_obj_add(struct net_device *dev, 277 const struct switchdev_obj *obj, 278 struct netlink_ext_ack *extack) 279 { 280 return -EOPNOTSUPP; 281 } 282 283 static inline int switchdev_port_obj_del(struct net_device *dev, 284 const struct switchdev_obj *obj) 285 { 286 return -EOPNOTSUPP; 287 } 288 289 static inline int register_switchdev_notifier(struct notifier_block *nb) 290 { 291 return 0; 292 } 293 294 static inline int unregister_switchdev_notifier(struct notifier_block *nb) 295 { 296 return 0; 297 } 298 299 static inline int call_switchdev_notifiers(unsigned long val, 300 struct net_device *dev, 301 struct switchdev_notifier_info *info, 302 struct netlink_ext_ack *extack) 303 { 304 return NOTIFY_DONE; 305 } 306 307 static inline int 308 register_switchdev_blocking_notifier(struct notifier_block *nb) 309 { 310 return 0; 311 } 312 313 static inline int 314 unregister_switchdev_blocking_notifier(struct notifier_block *nb) 315 { 316 return 0; 317 } 318 319 static inline int 320 call_switchdev_blocking_notifiers(unsigned long val, 321 struct net_device *dev, 322 struct switchdev_notifier_info *info, 323 struct netlink_ext_ack *extack) 324 { 325 return NOTIFY_DONE; 326 } 327 328 static inline int 329 switchdev_handle_port_obj_add(struct net_device *dev, 330 struct switchdev_notifier_port_obj_info *port_obj_info, 331 bool (*check_cb)(const struct net_device *dev), 332 int (*add_cb)(struct net_device *dev, 333 const struct switchdev_obj *obj, 334 struct switchdev_trans *trans, 335 struct netlink_ext_ack *extack)) 336 { 337 return 0; 338 } 339 340 static inline int 341 switchdev_handle_port_obj_del(struct net_device *dev, 342 struct switchdev_notifier_port_obj_info *port_obj_info, 343 bool (*check_cb)(const struct net_device *dev), 344 int (*del_cb)(struct net_device *dev, 345 const struct switchdev_obj *obj)) 346 { 347 return 0; 348 } 349 350 static inline int 351 switchdev_handle_port_attr_set(struct net_device *dev, 352 struct switchdev_notifier_port_attr_info *port_attr_info, 353 bool (*check_cb)(const struct net_device *dev), 354 int (*set_cb)(struct net_device *dev, 355 const struct switchdev_attr *attr, 356 struct switchdev_trans *trans)) 357 { 358 return 0; 359 } 360 #endif 361 362 #endif /* _LINUX_SWITCHDEV_H_ */ 363