Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * include/net/switchdev.h - Switch device API
0004  * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
0005  * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
0006  */
0007 #ifndef _LINUX_SWITCHDEV_H_
0008 #define _LINUX_SWITCHDEV_H_
0009 
0010 #include <linux/netdevice.h>
0011 #include <linux/notifier.h>
0012 #include <linux/list.h>
0013 #include <net/ip_fib.h>
0014 
0015 #define SWITCHDEV_F_NO_RECURSE      BIT(0)
0016 #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1)
0017 #define SWITCHDEV_F_DEFER       BIT(2)
0018 
0019 enum switchdev_attr_id {
0020     SWITCHDEV_ATTR_ID_UNDEFINED,
0021     SWITCHDEV_ATTR_ID_PORT_STP_STATE,
0022     SWITCHDEV_ATTR_ID_PORT_MST_STATE,
0023     SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
0024     SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
0025     SWITCHDEV_ATTR_ID_PORT_MROUTER,
0026     SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
0027     SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
0028     SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
0029     SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
0030     SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
0031     SWITCHDEV_ATTR_ID_BRIDGE_MST,
0032     SWITCHDEV_ATTR_ID_MRP_PORT_ROLE,
0033     SWITCHDEV_ATTR_ID_VLAN_MSTI,
0034 };
0035 
0036 struct switchdev_mst_state {
0037     u16 msti;
0038     u8 state;
0039 };
0040 
0041 struct switchdev_brport_flags {
0042     unsigned long val;
0043     unsigned long mask;
0044 };
0045 
0046 struct switchdev_vlan_msti {
0047     u16 vid;
0048     u16 msti;
0049 };
0050 
0051 struct switchdev_attr {
0052     struct net_device *orig_dev;
0053     enum switchdev_attr_id id;
0054     u32 flags;
0055     void *complete_priv;
0056     void (*complete)(struct net_device *dev, int err, void *priv);
0057     union {
0058         u8 stp_state;               /* PORT_STP_STATE */
0059         struct switchdev_mst_state mst_state;   /* PORT_MST_STATE */
0060         struct switchdev_brport_flags brport_flags; /* PORT_BRIDGE_FLAGS */
0061         bool mrouter;               /* PORT_MROUTER */
0062         clock_t ageing_time;            /* BRIDGE_AGEING_TIME */
0063         bool vlan_filtering;            /* BRIDGE_VLAN_FILTERING */
0064         u16 vlan_protocol;          /* BRIDGE_VLAN_PROTOCOL */
0065         bool mst;               /* BRIDGE_MST */
0066         bool mc_disabled;           /* MC_DISABLED */
0067         u8 mrp_port_role;           /* MRP_PORT_ROLE */
0068         struct switchdev_vlan_msti vlan_msti;   /* VLAN_MSTI */
0069     } u;
0070 };
0071 
0072 enum switchdev_obj_id {
0073     SWITCHDEV_OBJ_ID_UNDEFINED,
0074     SWITCHDEV_OBJ_ID_PORT_VLAN,
0075     SWITCHDEV_OBJ_ID_PORT_MDB,
0076     SWITCHDEV_OBJ_ID_HOST_MDB,
0077     SWITCHDEV_OBJ_ID_MRP,
0078     SWITCHDEV_OBJ_ID_RING_TEST_MRP,
0079     SWITCHDEV_OBJ_ID_RING_ROLE_MRP,
0080     SWITCHDEV_OBJ_ID_RING_STATE_MRP,
0081     SWITCHDEV_OBJ_ID_IN_TEST_MRP,
0082     SWITCHDEV_OBJ_ID_IN_ROLE_MRP,
0083     SWITCHDEV_OBJ_ID_IN_STATE_MRP,
0084 };
0085 
0086 struct switchdev_obj {
0087     struct list_head list;
0088     struct net_device *orig_dev;
0089     enum switchdev_obj_id id;
0090     u32 flags;
0091     void *complete_priv;
0092     void (*complete)(struct net_device *dev, int err, void *priv);
0093 };
0094 
0095 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
0096 struct switchdev_obj_port_vlan {
0097     struct switchdev_obj obj;
0098     u16 flags;
0099     u16 vid;
0100     /* If set, the notifier signifies a change of one of the following
0101      * flags for a VLAN that already exists:
0102      * - BRIDGE_VLAN_INFO_PVID
0103      * - BRIDGE_VLAN_INFO_UNTAGGED
0104      * Entries with BRIDGE_VLAN_INFO_BRENTRY unset are not notified at all.
0105      */
0106     bool changed;
0107 };
0108 
0109 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
0110     container_of((OBJ), struct switchdev_obj_port_vlan, obj)
0111 
0112 /* SWITCHDEV_OBJ_ID_PORT_MDB */
0113 struct switchdev_obj_port_mdb {
0114     struct switchdev_obj obj;
0115     unsigned char addr[ETH_ALEN];
0116     u16 vid;
0117 };
0118 
0119 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
0120     container_of((OBJ), struct switchdev_obj_port_mdb, obj)
0121 
0122 
0123 /* SWITCHDEV_OBJ_ID_MRP */
0124 struct switchdev_obj_mrp {
0125     struct switchdev_obj obj;
0126     struct net_device *p_port;
0127     struct net_device *s_port;
0128     u32 ring_id;
0129     u16 prio;
0130 };
0131 
0132 #define SWITCHDEV_OBJ_MRP(OBJ) \
0133     container_of((OBJ), struct switchdev_obj_mrp, obj)
0134 
0135 /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */
0136 struct switchdev_obj_ring_test_mrp {
0137     struct switchdev_obj obj;
0138     /* The value is in us and a value of 0 represents to stop */
0139     u32 interval;
0140     u8 max_miss;
0141     u32 ring_id;
0142     u32 period;
0143     bool monitor;
0144 };
0145 
0146 #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \
0147     container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj)
0148 
0149 /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */
0150 struct switchdev_obj_ring_role_mrp {
0151     struct switchdev_obj obj;
0152     u8 ring_role;
0153     u32 ring_id;
0154     u8 sw_backup;
0155 };
0156 
0157 #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
0158     container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
0159 
0160 struct switchdev_obj_ring_state_mrp {
0161     struct switchdev_obj obj;
0162     u8 ring_state;
0163     u32 ring_id;
0164 };
0165 
0166 #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
0167     container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
0168 
0169 /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
0170 struct switchdev_obj_in_test_mrp {
0171     struct switchdev_obj obj;
0172     /* The value is in us and a value of 0 represents to stop */
0173     u32 interval;
0174     u32 in_id;
0175     u32 period;
0176     u8 max_miss;
0177 };
0178 
0179 #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
0180     container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
0181 
0182 /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
0183 struct switchdev_obj_in_role_mrp {
0184     struct switchdev_obj obj;
0185     struct net_device *i_port;
0186     u32 ring_id;
0187     u16 in_id;
0188     u8 in_role;
0189     u8 sw_backup;
0190 };
0191 
0192 #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \
0193     container_of((OBJ), struct switchdev_obj_in_role_mrp, obj)
0194 
0195 struct switchdev_obj_in_state_mrp {
0196     struct switchdev_obj obj;
0197     u32 in_id;
0198     u8 in_state;
0199 };
0200 
0201 #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \
0202     container_of((OBJ), struct switchdev_obj_in_state_mrp, obj)
0203 
0204 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
0205 
0206 struct switchdev_brport {
0207     struct net_device *dev;
0208     const void *ctx;
0209     struct notifier_block *atomic_nb;
0210     struct notifier_block *blocking_nb;
0211     bool tx_fwd_offload;
0212 };
0213 
0214 enum switchdev_notifier_type {
0215     SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
0216     SWITCHDEV_FDB_DEL_TO_BRIDGE,
0217     SWITCHDEV_FDB_ADD_TO_DEVICE,
0218     SWITCHDEV_FDB_DEL_TO_DEVICE,
0219     SWITCHDEV_FDB_OFFLOADED,
0220     SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
0221 
0222     SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
0223     SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
0224     SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
0225 
0226     SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
0227     SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
0228     SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
0229     SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
0230     SWITCHDEV_VXLAN_FDB_OFFLOADED,
0231 
0232     SWITCHDEV_BRPORT_OFFLOADED,
0233     SWITCHDEV_BRPORT_UNOFFLOADED,
0234 };
0235 
0236 struct switchdev_notifier_info {
0237     struct net_device *dev;
0238     struct netlink_ext_ack *extack;
0239     const void *ctx;
0240 };
0241 
0242 /* Remember to update br_switchdev_fdb_populate() when adding
0243  * new members to this structure
0244  */
0245 struct switchdev_notifier_fdb_info {
0246     struct switchdev_notifier_info info; /* must be first */
0247     const unsigned char *addr;
0248     u16 vid;
0249     u8 added_by_user:1,
0250        is_local:1,
0251        offloaded:1;
0252 };
0253 
0254 struct switchdev_notifier_port_obj_info {
0255     struct switchdev_notifier_info info; /* must be first */
0256     const struct switchdev_obj *obj;
0257     bool handled;
0258 };
0259 
0260 struct switchdev_notifier_port_attr_info {
0261     struct switchdev_notifier_info info; /* must be first */
0262     const struct switchdev_attr *attr;
0263     bool handled;
0264 };
0265 
0266 struct switchdev_notifier_brport_info {
0267     struct switchdev_notifier_info info; /* must be first */
0268     const struct switchdev_brport brport;
0269 };
0270 
0271 static inline struct net_device *
0272 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
0273 {
0274     return info->dev;
0275 }
0276 
0277 static inline struct netlink_ext_ack *
0278 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
0279 {
0280     return info->extack;
0281 }
0282 
0283 static inline bool
0284 switchdev_fdb_is_dynamically_learned(const struct switchdev_notifier_fdb_info *fdb_info)
0285 {
0286     return !fdb_info->added_by_user && !fdb_info->is_local;
0287 }
0288 
0289 #ifdef CONFIG_NET_SWITCHDEV
0290 
0291 int switchdev_bridge_port_offload(struct net_device *brport_dev,
0292                   struct net_device *dev, const void *ctx,
0293                   struct notifier_block *atomic_nb,
0294                   struct notifier_block *blocking_nb,
0295                   bool tx_fwd_offload,
0296                   struct netlink_ext_ack *extack);
0297 void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
0298                      const void *ctx,
0299                      struct notifier_block *atomic_nb,
0300                      struct notifier_block *blocking_nb);
0301 
0302 void switchdev_deferred_process(void);
0303 int switchdev_port_attr_set(struct net_device *dev,
0304                 const struct switchdev_attr *attr,
0305                 struct netlink_ext_ack *extack);
0306 int switchdev_port_obj_add(struct net_device *dev,
0307                const struct switchdev_obj *obj,
0308                struct netlink_ext_ack *extack);
0309 int switchdev_port_obj_del(struct net_device *dev,
0310                const struct switchdev_obj *obj);
0311 
0312 int register_switchdev_notifier(struct notifier_block *nb);
0313 int unregister_switchdev_notifier(struct notifier_block *nb);
0314 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
0315                  struct switchdev_notifier_info *info,
0316                  struct netlink_ext_ack *extack);
0317 
0318 int register_switchdev_blocking_notifier(struct notifier_block *nb);
0319 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
0320 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
0321                       struct switchdev_notifier_info *info,
0322                       struct netlink_ext_ack *extack);
0323 
0324 void switchdev_port_fwd_mark_set(struct net_device *dev,
0325                  struct net_device *group_dev,
0326                  bool joining);
0327 
0328 int switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
0329         const struct switchdev_notifier_fdb_info *fdb_info,
0330         bool (*check_cb)(const struct net_device *dev),
0331         bool (*foreign_dev_check_cb)(const struct net_device *dev,
0332                          const struct net_device *foreign_dev),
0333         int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
0334                   unsigned long event, const void *ctx,
0335                   const struct switchdev_notifier_fdb_info *fdb_info));
0336 
0337 int switchdev_handle_port_obj_add(struct net_device *dev,
0338             struct switchdev_notifier_port_obj_info *port_obj_info,
0339             bool (*check_cb)(const struct net_device *dev),
0340             int (*add_cb)(struct net_device *dev, const void *ctx,
0341                       const struct switchdev_obj *obj,
0342                       struct netlink_ext_ack *extack));
0343 int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
0344             struct switchdev_notifier_port_obj_info *port_obj_info,
0345             bool (*check_cb)(const struct net_device *dev),
0346             bool (*foreign_dev_check_cb)(const struct net_device *dev,
0347                              const struct net_device *foreign_dev),
0348             int (*add_cb)(struct net_device *dev, const void *ctx,
0349                       const struct switchdev_obj *obj,
0350                       struct netlink_ext_ack *extack));
0351 int switchdev_handle_port_obj_del(struct net_device *dev,
0352             struct switchdev_notifier_port_obj_info *port_obj_info,
0353             bool (*check_cb)(const struct net_device *dev),
0354             int (*del_cb)(struct net_device *dev, const void *ctx,
0355                       const struct switchdev_obj *obj));
0356 int switchdev_handle_port_obj_del_foreign(struct net_device *dev,
0357             struct switchdev_notifier_port_obj_info *port_obj_info,
0358             bool (*check_cb)(const struct net_device *dev),
0359             bool (*foreign_dev_check_cb)(const struct net_device *dev,
0360                              const struct net_device *foreign_dev),
0361             int (*del_cb)(struct net_device *dev, const void *ctx,
0362                       const struct switchdev_obj *obj));
0363 
0364 int switchdev_handle_port_attr_set(struct net_device *dev,
0365             struct switchdev_notifier_port_attr_info *port_attr_info,
0366             bool (*check_cb)(const struct net_device *dev),
0367             int (*set_cb)(struct net_device *dev, const void *ctx,
0368                       const struct switchdev_attr *attr,
0369                       struct netlink_ext_ack *extack));
0370 #else
0371 
0372 static inline int
0373 switchdev_bridge_port_offload(struct net_device *brport_dev,
0374                   struct net_device *dev, const void *ctx,
0375                   struct notifier_block *atomic_nb,
0376                   struct notifier_block *blocking_nb,
0377                   bool tx_fwd_offload,
0378                   struct netlink_ext_ack *extack)
0379 {
0380     return -EOPNOTSUPP;
0381 }
0382 
0383 static inline void
0384 switchdev_bridge_port_unoffload(struct net_device *brport_dev,
0385                 const void *ctx,
0386                 struct notifier_block *atomic_nb,
0387                 struct notifier_block *blocking_nb)
0388 {
0389 }
0390 
0391 static inline void switchdev_deferred_process(void)
0392 {
0393 }
0394 
0395 static inline int switchdev_port_attr_set(struct net_device *dev,
0396                       const struct switchdev_attr *attr,
0397                       struct netlink_ext_ack *extack)
0398 {
0399     return -EOPNOTSUPP;
0400 }
0401 
0402 static inline int switchdev_port_obj_add(struct net_device *dev,
0403                      const struct switchdev_obj *obj,
0404                      struct netlink_ext_ack *extack)
0405 {
0406     return -EOPNOTSUPP;
0407 }
0408 
0409 static inline int switchdev_port_obj_del(struct net_device *dev,
0410                      const struct switchdev_obj *obj)
0411 {
0412     return -EOPNOTSUPP;
0413 }
0414 
0415 static inline int register_switchdev_notifier(struct notifier_block *nb)
0416 {
0417     return 0;
0418 }
0419 
0420 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
0421 {
0422     return 0;
0423 }
0424 
0425 static inline int call_switchdev_notifiers(unsigned long val,
0426                        struct net_device *dev,
0427                        struct switchdev_notifier_info *info,
0428                        struct netlink_ext_ack *extack)
0429 {
0430     return NOTIFY_DONE;
0431 }
0432 
0433 static inline int
0434 register_switchdev_blocking_notifier(struct notifier_block *nb)
0435 {
0436     return 0;
0437 }
0438 
0439 static inline int
0440 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
0441 {
0442     return 0;
0443 }
0444 
0445 static inline int
0446 call_switchdev_blocking_notifiers(unsigned long val,
0447                   struct net_device *dev,
0448                   struct switchdev_notifier_info *info,
0449                   struct netlink_ext_ack *extack)
0450 {
0451     return NOTIFY_DONE;
0452 }
0453 
0454 static inline int
0455 switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
0456         const struct switchdev_notifier_fdb_info *fdb_info,
0457         bool (*check_cb)(const struct net_device *dev),
0458         bool (*foreign_dev_check_cb)(const struct net_device *dev,
0459                          const struct net_device *foreign_dev),
0460         int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
0461                   unsigned long event, const void *ctx,
0462                   const struct switchdev_notifier_fdb_info *fdb_info))
0463 {
0464     return 0;
0465 }
0466 
0467 static inline int
0468 switchdev_handle_port_obj_add(struct net_device *dev,
0469             struct switchdev_notifier_port_obj_info *port_obj_info,
0470             bool (*check_cb)(const struct net_device *dev),
0471             int (*add_cb)(struct net_device *dev, const void *ctx,
0472                       const struct switchdev_obj *obj,
0473                       struct netlink_ext_ack *extack))
0474 {
0475     return 0;
0476 }
0477 
0478 static inline int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
0479             struct switchdev_notifier_port_obj_info *port_obj_info,
0480             bool (*check_cb)(const struct net_device *dev),
0481             bool (*foreign_dev_check_cb)(const struct net_device *dev,
0482                              const struct net_device *foreign_dev),
0483             int (*add_cb)(struct net_device *dev, const void *ctx,
0484                       const struct switchdev_obj *obj,
0485                       struct netlink_ext_ack *extack))
0486 {
0487     return 0;
0488 }
0489 
0490 static inline int
0491 switchdev_handle_port_obj_del(struct net_device *dev,
0492             struct switchdev_notifier_port_obj_info *port_obj_info,
0493             bool (*check_cb)(const struct net_device *dev),
0494             int (*del_cb)(struct net_device *dev, const void *ctx,
0495                       const struct switchdev_obj *obj))
0496 {
0497     return 0;
0498 }
0499 
0500 static inline int
0501 switchdev_handle_port_obj_del_foreign(struct net_device *dev,
0502             struct switchdev_notifier_port_obj_info *port_obj_info,
0503             bool (*check_cb)(const struct net_device *dev),
0504             bool (*foreign_dev_check_cb)(const struct net_device *dev,
0505                              const struct net_device *foreign_dev),
0506             int (*del_cb)(struct net_device *dev, const void *ctx,
0507                       const struct switchdev_obj *obj))
0508 {
0509     return 0;
0510 }
0511 
0512 static inline int
0513 switchdev_handle_port_attr_set(struct net_device *dev,
0514             struct switchdev_notifier_port_attr_info *port_attr_info,
0515             bool (*check_cb)(const struct net_device *dev),
0516             int (*set_cb)(struct net_device *dev, const void *ctx,
0517                       const struct switchdev_attr *attr,
0518                       struct netlink_ext_ack *extack))
0519 {
0520     return 0;
0521 }
0522 #endif
0523 
0524 #endif /* _LINUX_SWITCHDEV_H_ */