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_VLAN_PROTOCOL, 42 SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 43 SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 44 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 45 SWITCHDEV_ATTR_ID_MRP_PORT_STATE, 46 SWITCHDEV_ATTR_ID_MRP_PORT_ROLE, 47 #endif 48 }; 49 50 struct switchdev_attr { 51 struct net_device *orig_dev; 52 enum switchdev_attr_id id; 53 u32 flags; 54 void *complete_priv; 55 void (*complete)(struct net_device *dev, int err, void *priv); 56 union { 57 u8 stp_state; /* PORT_STP_STATE */ 58 unsigned long brport_flags; /* PORT_{PRE}_BRIDGE_FLAGS */ 59 bool mrouter; /* PORT_MROUTER */ 60 clock_t ageing_time; /* BRIDGE_AGEING_TIME */ 61 bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */ 62 u16 vlan_protocol; /* BRIDGE_VLAN_PROTOCOL */ 63 bool mc_disabled; /* MC_DISABLED */ 64 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 65 u8 mrp_port_state; /* MRP_PORT_STATE */ 66 u8 mrp_port_role; /* MRP_PORT_ROLE */ 67 #endif 68 } u; 69 }; 70 71 enum switchdev_obj_id { 72 SWITCHDEV_OBJ_ID_UNDEFINED, 73 SWITCHDEV_OBJ_ID_PORT_VLAN, 74 SWITCHDEV_OBJ_ID_PORT_MDB, 75 SWITCHDEV_OBJ_ID_HOST_MDB, 76 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 77 SWITCHDEV_OBJ_ID_MRP, 78 SWITCHDEV_OBJ_ID_RING_TEST_MRP, 79 SWITCHDEV_OBJ_ID_RING_ROLE_MRP, 80 SWITCHDEV_OBJ_ID_RING_STATE_MRP, 81 SWITCHDEV_OBJ_ID_IN_TEST_MRP, 82 SWITCHDEV_OBJ_ID_IN_ROLE_MRP, 83 SWITCHDEV_OBJ_ID_IN_STATE_MRP, 84 85 #endif 86 }; 87 88 struct switchdev_obj { 89 struct net_device *orig_dev; 90 enum switchdev_obj_id id; 91 u32 flags; 92 void *complete_priv; 93 void (*complete)(struct net_device *dev, int err, void *priv); 94 }; 95 96 /* SWITCHDEV_OBJ_ID_PORT_VLAN */ 97 struct switchdev_obj_port_vlan { 98 struct switchdev_obj obj; 99 u16 flags; 100 u16 vid; 101 }; 102 103 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \ 104 container_of((OBJ), struct switchdev_obj_port_vlan, obj) 105 106 /* SWITCHDEV_OBJ_ID_PORT_MDB */ 107 struct switchdev_obj_port_mdb { 108 struct switchdev_obj obj; 109 unsigned char addr[ETH_ALEN]; 110 u16 vid; 111 }; 112 113 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \ 114 container_of((OBJ), struct switchdev_obj_port_mdb, obj) 115 116 117 #if IS_ENABLED(CONFIG_BRIDGE_MRP) 118 /* SWITCHDEV_OBJ_ID_MRP */ 119 struct switchdev_obj_mrp { 120 struct switchdev_obj obj; 121 struct net_device *p_port; 122 struct net_device *s_port; 123 u32 ring_id; 124 u16 prio; 125 }; 126 127 #define SWITCHDEV_OBJ_MRP(OBJ) \ 128 container_of((OBJ), struct switchdev_obj_mrp, obj) 129 130 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */ 131 struct switchdev_obj_ring_test_mrp { 132 struct switchdev_obj obj; 133 /* The value is in us and a value of 0 represents to stop */ 134 u32 interval; 135 u8 max_miss; 136 u32 ring_id; 137 u32 period; 138 bool monitor; 139 }; 140 141 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \ 142 container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj) 143 144 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */ 145 struct switchdev_obj_ring_role_mrp { 146 struct switchdev_obj obj; 147 u8 ring_role; 148 u32 ring_id; 149 }; 150 151 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \ 152 container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj) 153 154 struct switchdev_obj_ring_state_mrp { 155 struct switchdev_obj obj; 156 u8 ring_state; 157 u32 ring_id; 158 }; 159 160 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \ 161 container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj) 162 163 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */ 164 struct switchdev_obj_in_test_mrp { 165 struct switchdev_obj obj; 166 /* The value is in us and a value of 0 represents to stop */ 167 u32 interval; 168 u32 in_id; 169 u32 period; 170 u8 max_miss; 171 }; 172 173 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \ 174 container_of((OBJ), struct switchdev_obj_in_test_mrp, obj) 175 176 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */ 177 struct switchdev_obj_in_role_mrp { 178 struct switchdev_obj obj; 179 struct net_device *i_port; 180 u32 ring_id; 181 u16 in_id; 182 u8 in_role; 183 }; 184 185 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \ 186 container_of((OBJ), struct switchdev_obj_in_role_mrp, obj) 187 188 struct switchdev_obj_in_state_mrp { 189 struct switchdev_obj obj; 190 u32 in_id; 191 u8 in_state; 192 }; 193 194 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \ 195 container_of((OBJ), struct switchdev_obj_in_state_mrp, obj) 196 197 #endif 198 199 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj); 200 201 enum switchdev_notifier_type { 202 SWITCHDEV_FDB_ADD_TO_BRIDGE = 1, 203 SWITCHDEV_FDB_DEL_TO_BRIDGE, 204 SWITCHDEV_FDB_ADD_TO_DEVICE, 205 SWITCHDEV_FDB_DEL_TO_DEVICE, 206 SWITCHDEV_FDB_OFFLOADED, 207 SWITCHDEV_FDB_FLUSH_TO_BRIDGE, 208 209 SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */ 210 SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */ 211 SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */ 212 213 SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE, 214 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE, 215 SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE, 216 SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE, 217 SWITCHDEV_VXLAN_FDB_OFFLOADED, 218 }; 219 220 struct switchdev_notifier_info { 221 struct net_device *dev; 222 struct netlink_ext_ack *extack; 223 }; 224 225 struct switchdev_notifier_fdb_info { 226 struct switchdev_notifier_info info; /* must be first */ 227 const unsigned char *addr; 228 u16 vid; 229 u8 added_by_user:1, 230 offloaded:1; 231 }; 232 233 struct switchdev_notifier_port_obj_info { 234 struct switchdev_notifier_info info; /* must be first */ 235 const struct switchdev_obj *obj; 236 bool handled; 237 }; 238 239 struct switchdev_notifier_port_attr_info { 240 struct switchdev_notifier_info info; /* must be first */ 241 const struct switchdev_attr *attr; 242 struct switchdev_trans *trans; 243 bool handled; 244 }; 245 246 static inline struct net_device * 247 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info) 248 { 249 return info->dev; 250 } 251 252 static inline struct netlink_ext_ack * 253 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info) 254 { 255 return info->extack; 256 } 257 258 #ifdef CONFIG_NET_SWITCHDEV 259 260 void switchdev_deferred_process(void); 261 int switchdev_port_attr_set(struct net_device *dev, 262 const struct switchdev_attr *attr); 263 int switchdev_port_obj_add(struct net_device *dev, 264 const struct switchdev_obj *obj, 265 struct netlink_ext_ack *extack); 266 int switchdev_port_obj_del(struct net_device *dev, 267 const struct switchdev_obj *obj); 268 269 int register_switchdev_notifier(struct notifier_block *nb); 270 int unregister_switchdev_notifier(struct notifier_block *nb); 271 int call_switchdev_notifiers(unsigned long val, struct net_device *dev, 272 struct switchdev_notifier_info *info, 273 struct netlink_ext_ack *extack); 274 275 int register_switchdev_blocking_notifier(struct notifier_block *nb); 276 int unregister_switchdev_blocking_notifier(struct notifier_block *nb); 277 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, 278 struct switchdev_notifier_info *info, 279 struct netlink_ext_ack *extack); 280 281 void switchdev_port_fwd_mark_set(struct net_device *dev, 282 struct net_device *group_dev, 283 bool joining); 284 285 int switchdev_handle_port_obj_add(struct net_device *dev, 286 struct switchdev_notifier_port_obj_info *port_obj_info, 287 bool (*check_cb)(const struct net_device *dev), 288 int (*add_cb)(struct net_device *dev, 289 const struct switchdev_obj *obj, 290 struct netlink_ext_ack *extack)); 291 int switchdev_handle_port_obj_del(struct net_device *dev, 292 struct switchdev_notifier_port_obj_info *port_obj_info, 293 bool (*check_cb)(const struct net_device *dev), 294 int (*del_cb)(struct net_device *dev, 295 const struct switchdev_obj *obj)); 296 297 int switchdev_handle_port_attr_set(struct net_device *dev, 298 struct switchdev_notifier_port_attr_info *port_attr_info, 299 bool (*check_cb)(const struct net_device *dev), 300 int (*set_cb)(struct net_device *dev, 301 const struct switchdev_attr *attr, 302 struct switchdev_trans *trans)); 303 #else 304 305 static inline void switchdev_deferred_process(void) 306 { 307 } 308 309 static inline int switchdev_port_attr_set(struct net_device *dev, 310 const struct switchdev_attr *attr) 311 { 312 return -EOPNOTSUPP; 313 } 314 315 static inline int switchdev_port_obj_add(struct net_device *dev, 316 const struct switchdev_obj *obj, 317 struct netlink_ext_ack *extack) 318 { 319 return -EOPNOTSUPP; 320 } 321 322 static inline int switchdev_port_obj_del(struct net_device *dev, 323 const struct switchdev_obj *obj) 324 { 325 return -EOPNOTSUPP; 326 } 327 328 static inline int register_switchdev_notifier(struct notifier_block *nb) 329 { 330 return 0; 331 } 332 333 static inline int unregister_switchdev_notifier(struct notifier_block *nb) 334 { 335 return 0; 336 } 337 338 static inline int call_switchdev_notifiers(unsigned long val, 339 struct net_device *dev, 340 struct switchdev_notifier_info *info, 341 struct netlink_ext_ack *extack) 342 { 343 return NOTIFY_DONE; 344 } 345 346 static inline int 347 register_switchdev_blocking_notifier(struct notifier_block *nb) 348 { 349 return 0; 350 } 351 352 static inline int 353 unregister_switchdev_blocking_notifier(struct notifier_block *nb) 354 { 355 return 0; 356 } 357 358 static inline int 359 call_switchdev_blocking_notifiers(unsigned long val, 360 struct net_device *dev, 361 struct switchdev_notifier_info *info, 362 struct netlink_ext_ack *extack) 363 { 364 return NOTIFY_DONE; 365 } 366 367 static inline int 368 switchdev_handle_port_obj_add(struct net_device *dev, 369 struct switchdev_notifier_port_obj_info *port_obj_info, 370 bool (*check_cb)(const struct net_device *dev), 371 int (*add_cb)(struct net_device *dev, 372 const struct switchdev_obj *obj, 373 struct netlink_ext_ack *extack)) 374 { 375 return 0; 376 } 377 378 static inline int 379 switchdev_handle_port_obj_del(struct net_device *dev, 380 struct switchdev_notifier_port_obj_info *port_obj_info, 381 bool (*check_cb)(const struct net_device *dev), 382 int (*del_cb)(struct net_device *dev, 383 const struct switchdev_obj *obj)) 384 { 385 return 0; 386 } 387 388 static inline int 389 switchdev_handle_port_attr_set(struct net_device *dev, 390 struct switchdev_notifier_port_attr_info *port_attr_info, 391 bool (*check_cb)(const struct net_device *dev), 392 int (*set_cb)(struct net_device *dev, 393 const struct switchdev_attr *attr, 394 struct switchdev_trans *trans)) 395 { 396 return 0; 397 } 398 #endif 399 400 #endif /* _LINUX_SWITCHDEV_H_ */ 401