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 enum switchdev_attr_id { 20 SWITCHDEV_ATTR_ID_UNDEFINED, 21 SWITCHDEV_ATTR_ID_PORT_STP_STATE, 22 SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS, 23 SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS, 24 SWITCHDEV_ATTR_ID_PORT_MROUTER, 25 SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME, 26 SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING, 27 SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL, 28 SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, 29 SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, 30 SWITCHDEV_ATTR_ID_MRP_PORT_ROLE, 31 }; 32 33 struct switchdev_brport_flags { 34 unsigned long val; 35 unsigned long mask; 36 }; 37 38 struct switchdev_attr { 39 struct net_device *orig_dev; 40 enum switchdev_attr_id id; 41 u32 flags; 42 void *complete_priv; 43 void (*complete)(struct net_device *dev, int err, void *priv); 44 union { 45 u8 stp_state; /* PORT_STP_STATE */ 46 struct switchdev_brport_flags brport_flags; /* PORT_BRIDGE_FLAGS */ 47 bool mrouter; /* PORT_MROUTER */ 48 clock_t ageing_time; /* BRIDGE_AGEING_TIME */ 49 bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */ 50 u16 vlan_protocol; /* BRIDGE_VLAN_PROTOCOL */ 51 bool mc_disabled; /* MC_DISABLED */ 52 u8 mrp_port_role; /* MRP_PORT_ROLE */ 53 } u; 54 }; 55 56 enum switchdev_obj_id { 57 SWITCHDEV_OBJ_ID_UNDEFINED, 58 SWITCHDEV_OBJ_ID_PORT_VLAN, 59 SWITCHDEV_OBJ_ID_PORT_MDB, 60 SWITCHDEV_OBJ_ID_HOST_MDB, 61 SWITCHDEV_OBJ_ID_MRP, 62 SWITCHDEV_OBJ_ID_RING_TEST_MRP, 63 SWITCHDEV_OBJ_ID_RING_ROLE_MRP, 64 SWITCHDEV_OBJ_ID_RING_STATE_MRP, 65 SWITCHDEV_OBJ_ID_IN_TEST_MRP, 66 SWITCHDEV_OBJ_ID_IN_ROLE_MRP, 67 SWITCHDEV_OBJ_ID_IN_STATE_MRP, 68 }; 69 70 struct switchdev_obj { 71 struct list_head list; 72 struct net_device *orig_dev; 73 enum switchdev_obj_id id; 74 u32 flags; 75 void *complete_priv; 76 void (*complete)(struct net_device *dev, int err, void *priv); 77 }; 78 79 /* SWITCHDEV_OBJ_ID_PORT_VLAN */ 80 struct switchdev_obj_port_vlan { 81 struct switchdev_obj obj; 82 u16 flags; 83 u16 vid; 84 /* If set, the notifier signifies a change of one of the following 85 * flags for a VLAN that already exists: 86 * - BRIDGE_VLAN_INFO_PVID 87 * - BRIDGE_VLAN_INFO_UNTAGGED 88 * Entries with BRIDGE_VLAN_INFO_BRENTRY unset are not notified at all. 89 */ 90 bool changed; 91 }; 92 93 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \ 94 container_of((OBJ), struct switchdev_obj_port_vlan, obj) 95 96 /* SWITCHDEV_OBJ_ID_PORT_MDB */ 97 struct switchdev_obj_port_mdb { 98 struct switchdev_obj obj; 99 unsigned char addr[ETH_ALEN]; 100 u16 vid; 101 }; 102 103 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \ 104 container_of((OBJ), struct switchdev_obj_port_mdb, obj) 105 106 107 /* SWITCHDEV_OBJ_ID_MRP */ 108 struct switchdev_obj_mrp { 109 struct switchdev_obj obj; 110 struct net_device *p_port; 111 struct net_device *s_port; 112 u32 ring_id; 113 u16 prio; 114 }; 115 116 #define SWITCHDEV_OBJ_MRP(OBJ) \ 117 container_of((OBJ), struct switchdev_obj_mrp, obj) 118 119 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */ 120 struct switchdev_obj_ring_test_mrp { 121 struct switchdev_obj obj; 122 /* The value is in us and a value of 0 represents to stop */ 123 u32 interval; 124 u8 max_miss; 125 u32 ring_id; 126 u32 period; 127 bool monitor; 128 }; 129 130 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \ 131 container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj) 132 133 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */ 134 struct switchdev_obj_ring_role_mrp { 135 struct switchdev_obj obj; 136 u8 ring_role; 137 u32 ring_id; 138 u8 sw_backup; 139 }; 140 141 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \ 142 container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj) 143 144 struct switchdev_obj_ring_state_mrp { 145 struct switchdev_obj obj; 146 u8 ring_state; 147 u32 ring_id; 148 }; 149 150 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \ 151 container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj) 152 153 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */ 154 struct switchdev_obj_in_test_mrp { 155 struct switchdev_obj obj; 156 /* The value is in us and a value of 0 represents to stop */ 157 u32 interval; 158 u32 in_id; 159 u32 period; 160 u8 max_miss; 161 }; 162 163 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \ 164 container_of((OBJ), struct switchdev_obj_in_test_mrp, obj) 165 166 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */ 167 struct switchdev_obj_in_role_mrp { 168 struct switchdev_obj obj; 169 struct net_device *i_port; 170 u32 ring_id; 171 u16 in_id; 172 u8 in_role; 173 u8 sw_backup; 174 }; 175 176 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \ 177 container_of((OBJ), struct switchdev_obj_in_role_mrp, obj) 178 179 struct switchdev_obj_in_state_mrp { 180 struct switchdev_obj obj; 181 u32 in_id; 182 u8 in_state; 183 }; 184 185 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \ 186 container_of((OBJ), struct switchdev_obj_in_state_mrp, obj) 187 188 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj); 189 190 struct switchdev_brport { 191 struct net_device *dev; 192 const void *ctx; 193 struct notifier_block *atomic_nb; 194 struct notifier_block *blocking_nb; 195 bool tx_fwd_offload; 196 }; 197 198 enum switchdev_notifier_type { 199 SWITCHDEV_FDB_ADD_TO_BRIDGE = 1, 200 SWITCHDEV_FDB_DEL_TO_BRIDGE, 201 SWITCHDEV_FDB_ADD_TO_DEVICE, 202 SWITCHDEV_FDB_DEL_TO_DEVICE, 203 SWITCHDEV_FDB_OFFLOADED, 204 SWITCHDEV_FDB_FLUSH_TO_BRIDGE, 205 206 SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */ 207 SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */ 208 SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */ 209 210 SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE, 211 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE, 212 SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE, 213 SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE, 214 SWITCHDEV_VXLAN_FDB_OFFLOADED, 215 216 SWITCHDEV_BRPORT_OFFLOADED, 217 SWITCHDEV_BRPORT_UNOFFLOADED, 218 }; 219 220 struct switchdev_notifier_info { 221 struct net_device *dev; 222 struct netlink_ext_ack *extack; 223 const void *ctx; 224 }; 225 226 struct switchdev_notifier_fdb_info { 227 struct switchdev_notifier_info info; /* must be first */ 228 const unsigned char *addr; 229 u16 vid; 230 u8 added_by_user:1, 231 is_local:1, 232 offloaded:1; 233 }; 234 235 struct switchdev_notifier_port_obj_info { 236 struct switchdev_notifier_info info; /* must be first */ 237 const struct switchdev_obj *obj; 238 bool handled; 239 }; 240 241 struct switchdev_notifier_port_attr_info { 242 struct switchdev_notifier_info info; /* must be first */ 243 const struct switchdev_attr *attr; 244 bool handled; 245 }; 246 247 struct switchdev_notifier_brport_info { 248 struct switchdev_notifier_info info; /* must be first */ 249 const struct switchdev_brport brport; 250 }; 251 252 static inline struct net_device * 253 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info) 254 { 255 return info->dev; 256 } 257 258 static inline struct netlink_ext_ack * 259 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info) 260 { 261 return info->extack; 262 } 263 264 static inline bool 265 switchdev_fdb_is_dynamically_learned(const struct switchdev_notifier_fdb_info *fdb_info) 266 { 267 return !fdb_info->added_by_user && !fdb_info->is_local; 268 } 269 270 #ifdef CONFIG_NET_SWITCHDEV 271 272 int switchdev_bridge_port_offload(struct net_device *brport_dev, 273 struct net_device *dev, const void *ctx, 274 struct notifier_block *atomic_nb, 275 struct notifier_block *blocking_nb, 276 bool tx_fwd_offload, 277 struct netlink_ext_ack *extack); 278 void switchdev_bridge_port_unoffload(struct net_device *brport_dev, 279 const void *ctx, 280 struct notifier_block *atomic_nb, 281 struct notifier_block *blocking_nb); 282 283 void switchdev_deferred_process(void); 284 int switchdev_port_attr_set(struct net_device *dev, 285 const struct switchdev_attr *attr, 286 struct netlink_ext_ack *extack); 287 int switchdev_port_obj_add(struct net_device *dev, 288 const struct switchdev_obj *obj, 289 struct netlink_ext_ack *extack); 290 int switchdev_port_obj_del(struct net_device *dev, 291 const struct switchdev_obj *obj); 292 293 int register_switchdev_notifier(struct notifier_block *nb); 294 int unregister_switchdev_notifier(struct notifier_block *nb); 295 int call_switchdev_notifiers(unsigned long val, struct net_device *dev, 296 struct switchdev_notifier_info *info, 297 struct netlink_ext_ack *extack); 298 299 int register_switchdev_blocking_notifier(struct notifier_block *nb); 300 int unregister_switchdev_blocking_notifier(struct notifier_block *nb); 301 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, 302 struct switchdev_notifier_info *info, 303 struct netlink_ext_ack *extack); 304 305 void switchdev_port_fwd_mark_set(struct net_device *dev, 306 struct net_device *group_dev, 307 bool joining); 308 309 int switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event, 310 const struct switchdev_notifier_fdb_info *fdb_info, 311 bool (*check_cb)(const struct net_device *dev), 312 bool (*foreign_dev_check_cb)(const struct net_device *dev, 313 const struct net_device *foreign_dev), 314 int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev, 315 unsigned long event, const void *ctx, 316 const struct switchdev_notifier_fdb_info *fdb_info)); 317 318 int switchdev_handle_port_obj_add(struct net_device *dev, 319 struct switchdev_notifier_port_obj_info *port_obj_info, 320 bool (*check_cb)(const struct net_device *dev), 321 int (*add_cb)(struct net_device *dev, const void *ctx, 322 const struct switchdev_obj *obj, 323 struct netlink_ext_ack *extack)); 324 int switchdev_handle_port_obj_add_foreign(struct net_device *dev, 325 struct switchdev_notifier_port_obj_info *port_obj_info, 326 bool (*check_cb)(const struct net_device *dev), 327 bool (*foreign_dev_check_cb)(const struct net_device *dev, 328 const struct net_device *foreign_dev), 329 int (*add_cb)(struct net_device *dev, const void *ctx, 330 const struct switchdev_obj *obj, 331 struct netlink_ext_ack *extack)); 332 int switchdev_handle_port_obj_del(struct net_device *dev, 333 struct switchdev_notifier_port_obj_info *port_obj_info, 334 bool (*check_cb)(const struct net_device *dev), 335 int (*del_cb)(struct net_device *dev, const void *ctx, 336 const struct switchdev_obj *obj)); 337 int switchdev_handle_port_obj_del_foreign(struct net_device *dev, 338 struct switchdev_notifier_port_obj_info *port_obj_info, 339 bool (*check_cb)(const struct net_device *dev), 340 bool (*foreign_dev_check_cb)(const struct net_device *dev, 341 const struct net_device *foreign_dev), 342 int (*del_cb)(struct net_device *dev, const void *ctx, 343 const struct switchdev_obj *obj)); 344 345 int switchdev_handle_port_attr_set(struct net_device *dev, 346 struct switchdev_notifier_port_attr_info *port_attr_info, 347 bool (*check_cb)(const struct net_device *dev), 348 int (*set_cb)(struct net_device *dev, const void *ctx, 349 const struct switchdev_attr *attr, 350 struct netlink_ext_ack *extack)); 351 #else 352 353 static inline int 354 switchdev_bridge_port_offload(struct net_device *brport_dev, 355 struct net_device *dev, const void *ctx, 356 struct notifier_block *atomic_nb, 357 struct notifier_block *blocking_nb, 358 bool tx_fwd_offload, 359 struct netlink_ext_ack *extack) 360 { 361 return -EOPNOTSUPP; 362 } 363 364 static inline void 365 switchdev_bridge_port_unoffload(struct net_device *brport_dev, 366 const void *ctx, 367 struct notifier_block *atomic_nb, 368 struct notifier_block *blocking_nb) 369 { 370 } 371 372 static inline void switchdev_deferred_process(void) 373 { 374 } 375 376 static inline int switchdev_port_attr_set(struct net_device *dev, 377 const struct switchdev_attr *attr, 378 struct netlink_ext_ack *extack) 379 { 380 return -EOPNOTSUPP; 381 } 382 383 static inline int switchdev_port_obj_add(struct net_device *dev, 384 const struct switchdev_obj *obj, 385 struct netlink_ext_ack *extack) 386 { 387 return -EOPNOTSUPP; 388 } 389 390 static inline int switchdev_port_obj_del(struct net_device *dev, 391 const struct switchdev_obj *obj) 392 { 393 return -EOPNOTSUPP; 394 } 395 396 static inline int register_switchdev_notifier(struct notifier_block *nb) 397 { 398 return 0; 399 } 400 401 static inline int unregister_switchdev_notifier(struct notifier_block *nb) 402 { 403 return 0; 404 } 405 406 static inline int call_switchdev_notifiers(unsigned long val, 407 struct net_device *dev, 408 struct switchdev_notifier_info *info, 409 struct netlink_ext_ack *extack) 410 { 411 return NOTIFY_DONE; 412 } 413 414 static inline int 415 register_switchdev_blocking_notifier(struct notifier_block *nb) 416 { 417 return 0; 418 } 419 420 static inline int 421 unregister_switchdev_blocking_notifier(struct notifier_block *nb) 422 { 423 return 0; 424 } 425 426 static inline int 427 call_switchdev_blocking_notifiers(unsigned long val, 428 struct net_device *dev, 429 struct switchdev_notifier_info *info, 430 struct netlink_ext_ack *extack) 431 { 432 return NOTIFY_DONE; 433 } 434 435 static inline int 436 switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event, 437 const struct switchdev_notifier_fdb_info *fdb_info, 438 bool (*check_cb)(const struct net_device *dev), 439 bool (*foreign_dev_check_cb)(const struct net_device *dev, 440 const struct net_device *foreign_dev), 441 int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev, 442 unsigned long event, const void *ctx, 443 const struct switchdev_notifier_fdb_info *fdb_info)) 444 { 445 return 0; 446 } 447 448 static inline int 449 switchdev_handle_port_obj_add(struct net_device *dev, 450 struct switchdev_notifier_port_obj_info *port_obj_info, 451 bool (*check_cb)(const struct net_device *dev), 452 int (*add_cb)(struct net_device *dev, const void *ctx, 453 const struct switchdev_obj *obj, 454 struct netlink_ext_ack *extack)) 455 { 456 return 0; 457 } 458 459 static inline int switchdev_handle_port_obj_add_foreign(struct net_device *dev, 460 struct switchdev_notifier_port_obj_info *port_obj_info, 461 bool (*check_cb)(const struct net_device *dev), 462 bool (*foreign_dev_check_cb)(const struct net_device *dev, 463 const struct net_device *foreign_dev), 464 int (*add_cb)(struct net_device *dev, const void *ctx, 465 const struct switchdev_obj *obj, 466 struct netlink_ext_ack *extack)) 467 { 468 return 0; 469 } 470 471 static inline int 472 switchdev_handle_port_obj_del(struct net_device *dev, 473 struct switchdev_notifier_port_obj_info *port_obj_info, 474 bool (*check_cb)(const struct net_device *dev), 475 int (*del_cb)(struct net_device *dev, const void *ctx, 476 const struct switchdev_obj *obj)) 477 { 478 return 0; 479 } 480 481 static inline int 482 switchdev_handle_port_obj_del_foreign(struct net_device *dev, 483 struct switchdev_notifier_port_obj_info *port_obj_info, 484 bool (*check_cb)(const struct net_device *dev), 485 bool (*foreign_dev_check_cb)(const struct net_device *dev, 486 const struct net_device *foreign_dev), 487 int (*del_cb)(struct net_device *dev, const void *ctx, 488 const struct switchdev_obj *obj)) 489 { 490 return 0; 491 } 492 493 static inline int 494 switchdev_handle_port_attr_set(struct net_device *dev, 495 struct switchdev_notifier_port_attr_info *port_attr_info, 496 bool (*check_cb)(const struct net_device *dev), 497 int (*set_cb)(struct net_device *dev, const void *ctx, 498 const struct switchdev_attr *attr, 499 struct netlink_ext_ack *extack)) 500 { 501 return 0; 502 } 503 #endif 504 505 #endif /* _LINUX_SWITCHDEV_H_ */ 506