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 struct switchdev_trans *trans; 237 bool handled; 238 }; 239 240 struct switchdev_notifier_port_attr_info { 241 struct switchdev_notifier_info info; /* must be first */ 242 const struct switchdev_attr *attr; 243 struct switchdev_trans *trans; 244 bool handled; 245 }; 246 247 static inline struct net_device * 248 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info) 249 { 250 return info->dev; 251 } 252 253 static inline struct netlink_ext_ack * 254 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info) 255 { 256 return info->extack; 257 } 258 259 #ifdef CONFIG_NET_SWITCHDEV 260 261 void switchdev_deferred_process(void); 262 int switchdev_port_attr_set(struct net_device *dev, 263 const struct switchdev_attr *attr); 264 int switchdev_port_obj_add(struct net_device *dev, 265 const struct switchdev_obj *obj, 266 struct netlink_ext_ack *extack); 267 int switchdev_port_obj_del(struct net_device *dev, 268 const struct switchdev_obj *obj); 269 270 int register_switchdev_notifier(struct notifier_block *nb); 271 int unregister_switchdev_notifier(struct notifier_block *nb); 272 int call_switchdev_notifiers(unsigned long val, struct net_device *dev, 273 struct switchdev_notifier_info *info, 274 struct netlink_ext_ack *extack); 275 276 int register_switchdev_blocking_notifier(struct notifier_block *nb); 277 int unregister_switchdev_blocking_notifier(struct notifier_block *nb); 278 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev, 279 struct switchdev_notifier_info *info, 280 struct netlink_ext_ack *extack); 281 282 void switchdev_port_fwd_mark_set(struct net_device *dev, 283 struct net_device *group_dev, 284 bool joining); 285 286 int switchdev_handle_port_obj_add(struct net_device *dev, 287 struct switchdev_notifier_port_obj_info *port_obj_info, 288 bool (*check_cb)(const struct net_device *dev), 289 int (*add_cb)(struct net_device *dev, 290 const struct switchdev_obj *obj, 291 struct switchdev_trans *trans, 292 struct netlink_ext_ack *extack)); 293 int switchdev_handle_port_obj_del(struct net_device *dev, 294 struct switchdev_notifier_port_obj_info *port_obj_info, 295 bool (*check_cb)(const struct net_device *dev), 296 int (*del_cb)(struct net_device *dev, 297 const struct switchdev_obj *obj)); 298 299 int switchdev_handle_port_attr_set(struct net_device *dev, 300 struct switchdev_notifier_port_attr_info *port_attr_info, 301 bool (*check_cb)(const struct net_device *dev), 302 int (*set_cb)(struct net_device *dev, 303 const struct switchdev_attr *attr, 304 struct switchdev_trans *trans)); 305 #else 306 307 static inline void switchdev_deferred_process(void) 308 { 309 } 310 311 static inline int switchdev_port_attr_set(struct net_device *dev, 312 const struct switchdev_attr *attr) 313 { 314 return -EOPNOTSUPP; 315 } 316 317 static inline int switchdev_port_obj_add(struct net_device *dev, 318 const struct switchdev_obj *obj, 319 struct netlink_ext_ack *extack) 320 { 321 return -EOPNOTSUPP; 322 } 323 324 static inline int switchdev_port_obj_del(struct net_device *dev, 325 const struct switchdev_obj *obj) 326 { 327 return -EOPNOTSUPP; 328 } 329 330 static inline int register_switchdev_notifier(struct notifier_block *nb) 331 { 332 return 0; 333 } 334 335 static inline int unregister_switchdev_notifier(struct notifier_block *nb) 336 { 337 return 0; 338 } 339 340 static inline int call_switchdev_notifiers(unsigned long val, 341 struct net_device *dev, 342 struct switchdev_notifier_info *info, 343 struct netlink_ext_ack *extack) 344 { 345 return NOTIFY_DONE; 346 } 347 348 static inline int 349 register_switchdev_blocking_notifier(struct notifier_block *nb) 350 { 351 return 0; 352 } 353 354 static inline int 355 unregister_switchdev_blocking_notifier(struct notifier_block *nb) 356 { 357 return 0; 358 } 359 360 static inline int 361 call_switchdev_blocking_notifiers(unsigned long val, 362 struct net_device *dev, 363 struct switchdev_notifier_info *info, 364 struct netlink_ext_ack *extack) 365 { 366 return NOTIFY_DONE; 367 } 368 369 static inline int 370 switchdev_handle_port_obj_add(struct net_device *dev, 371 struct switchdev_notifier_port_obj_info *port_obj_info, 372 bool (*check_cb)(const struct net_device *dev), 373 int (*add_cb)(struct net_device *dev, 374 const struct switchdev_obj *obj, 375 struct switchdev_trans *trans, 376 struct netlink_ext_ack *extack)) 377 { 378 return 0; 379 } 380 381 static inline int 382 switchdev_handle_port_obj_del(struct net_device *dev, 383 struct switchdev_notifier_port_obj_info *port_obj_info, 384 bool (*check_cb)(const struct net_device *dev), 385 int (*del_cb)(struct net_device *dev, 386 const struct switchdev_obj *obj)) 387 { 388 return 0; 389 } 390 391 static inline int 392 switchdev_handle_port_attr_set(struct net_device *dev, 393 struct switchdev_notifier_port_attr_info *port_attr_info, 394 bool (*check_cb)(const struct net_device *dev), 395 int (*set_cb)(struct net_device *dev, 396 const struct switchdev_attr *attr, 397 struct switchdev_trans *trans)) 398 { 399 return 0; 400 } 401 #endif 402 403 #endif /* _LINUX_SWITCHDEV_H_ */ 404