Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * include/linux/if_team.h - Network team device driver header
0004  * Copyright (c) 2011 Jiri Pirko <jpirko@redhat.com>
0005  */
0006 #ifndef _LINUX_IF_TEAM_H_
0007 #define _LINUX_IF_TEAM_H_
0008 
0009 #include <linux/netpoll.h>
0010 #include <net/sch_generic.h>
0011 #include <linux/types.h>
0012 #include <uapi/linux/if_team.h>
0013 
0014 struct team_pcpu_stats {
0015     u64_stats_t     rx_packets;
0016     u64_stats_t     rx_bytes;
0017     u64_stats_t     rx_multicast;
0018     u64_stats_t     tx_packets;
0019     u64_stats_t     tx_bytes;
0020     struct u64_stats_sync   syncp;
0021     u32         rx_dropped;
0022     u32         tx_dropped;
0023     u32         rx_nohandler;
0024 };
0025 
0026 struct team;
0027 
0028 struct team_port {
0029     struct net_device *dev;
0030     struct hlist_node hlist; /* node in enabled ports hash list */
0031     struct list_head list; /* node in ordinary list */
0032     struct team *team;
0033     int index; /* index of enabled port. If disabled, it's set to -1 */
0034 
0035     bool linkup; /* either state.linkup or user.linkup */
0036 
0037     struct {
0038         bool linkup;
0039         u32 speed;
0040         u8 duplex;
0041     } state;
0042 
0043     /* Values set by userspace */
0044     struct {
0045         bool linkup;
0046         bool linkup_enabled;
0047     } user;
0048 
0049     /* Custom gennetlink interface related flags */
0050     bool changed;
0051     bool removed;
0052 
0053     /*
0054      * A place for storing original values of the device before it
0055      * become a port.
0056      */
0057     struct {
0058         unsigned char dev_addr[MAX_ADDR_LEN];
0059         unsigned int mtu;
0060     } orig;
0061 
0062 #ifdef CONFIG_NET_POLL_CONTROLLER
0063     struct netpoll *np;
0064 #endif
0065 
0066     s32 priority; /* lower number ~ higher priority */
0067     u16 queue_id;
0068     struct list_head qom_list; /* node in queue override mapping list */
0069     struct rcu_head rcu;
0070     long mode_priv[];
0071 };
0072 
0073 static inline struct team_port *team_port_get_rcu(const struct net_device *dev)
0074 {
0075     return rcu_dereference(dev->rx_handler_data);
0076 }
0077 
0078 static inline bool team_port_enabled(struct team_port *port)
0079 {
0080     return port->index != -1;
0081 }
0082 
0083 static inline bool team_port_txable(struct team_port *port)
0084 {
0085     return port->linkup && team_port_enabled(port);
0086 }
0087 
0088 static inline bool team_port_dev_txable(const struct net_device *port_dev)
0089 {
0090     struct team_port *port;
0091     bool txable;
0092 
0093     rcu_read_lock();
0094     port = team_port_get_rcu(port_dev);
0095     txable = port ? team_port_txable(port) : false;
0096     rcu_read_unlock();
0097 
0098     return txable;
0099 }
0100 
0101 #ifdef CONFIG_NET_POLL_CONTROLLER
0102 static inline void team_netpoll_send_skb(struct team_port *port,
0103                      struct sk_buff *skb)
0104 {
0105     netpoll_send_skb(port->np, skb);
0106 }
0107 #else
0108 static inline void team_netpoll_send_skb(struct team_port *port,
0109                      struct sk_buff *skb)
0110 {
0111 }
0112 #endif
0113 
0114 struct team_mode_ops {
0115     int (*init)(struct team *team);
0116     void (*exit)(struct team *team);
0117     rx_handler_result_t (*receive)(struct team *team,
0118                        struct team_port *port,
0119                        struct sk_buff *skb);
0120     bool (*transmit)(struct team *team, struct sk_buff *skb);
0121     int (*port_enter)(struct team *team, struct team_port *port);
0122     void (*port_leave)(struct team *team, struct team_port *port);
0123     void (*port_change_dev_addr)(struct team *team, struct team_port *port);
0124     void (*port_enabled)(struct team *team, struct team_port *port);
0125     void (*port_disabled)(struct team *team, struct team_port *port);
0126 };
0127 
0128 extern int team_modeop_port_enter(struct team *team, struct team_port *port);
0129 extern void team_modeop_port_change_dev_addr(struct team *team,
0130                          struct team_port *port);
0131 
0132 enum team_option_type {
0133     TEAM_OPTION_TYPE_U32,
0134     TEAM_OPTION_TYPE_STRING,
0135     TEAM_OPTION_TYPE_BINARY,
0136     TEAM_OPTION_TYPE_BOOL,
0137     TEAM_OPTION_TYPE_S32,
0138 };
0139 
0140 struct team_option_inst_info {
0141     u32 array_index;
0142     struct team_port *port; /* != NULL if per-port */
0143 };
0144 
0145 struct team_gsetter_ctx {
0146     union {
0147         u32 u32_val;
0148         const char *str_val;
0149         struct {
0150             const void *ptr;
0151             u32 len;
0152         } bin_val;
0153         bool bool_val;
0154         s32 s32_val;
0155     } data;
0156     struct team_option_inst_info *info;
0157 };
0158 
0159 struct team_option {
0160     struct list_head list;
0161     const char *name;
0162     bool per_port;
0163     unsigned int array_size; /* != 0 means the option is array */
0164     enum team_option_type type;
0165     int (*init)(struct team *team, struct team_option_inst_info *info);
0166     int (*getter)(struct team *team, struct team_gsetter_ctx *ctx);
0167     int (*setter)(struct team *team, struct team_gsetter_ctx *ctx);
0168 };
0169 
0170 extern void team_option_inst_set_change(struct team_option_inst_info *opt_inst_info);
0171 extern void team_options_change_check(struct team *team);
0172 
0173 struct team_mode {
0174     const char *kind;
0175     struct module *owner;
0176     size_t priv_size;
0177     size_t port_priv_size;
0178     const struct team_mode_ops *ops;
0179     enum netdev_lag_tx_type lag_tx_type;
0180 };
0181 
0182 #define TEAM_PORT_HASHBITS 4
0183 #define TEAM_PORT_HASHENTRIES (1 << TEAM_PORT_HASHBITS)
0184 
0185 #define TEAM_MODE_PRIV_LONGS 4
0186 #define TEAM_MODE_PRIV_SIZE (sizeof(long) * TEAM_MODE_PRIV_LONGS)
0187 
0188 struct team {
0189     struct net_device *dev; /* associated netdevice */
0190     struct team_pcpu_stats __percpu *pcpu_stats;
0191 
0192     struct mutex lock; /* used for overall locking, e.g. port lists write */
0193 
0194     /*
0195      * List of enabled ports and their count
0196      */
0197     int en_port_count;
0198     struct hlist_head en_port_hlist[TEAM_PORT_HASHENTRIES];
0199 
0200     struct list_head port_list; /* list of all ports */
0201 
0202     struct list_head option_list;
0203     struct list_head option_inst_list; /* list of option instances */
0204 
0205     const struct team_mode *mode;
0206     struct team_mode_ops ops;
0207     bool user_carrier_enabled;
0208     bool queue_override_enabled;
0209     struct list_head *qom_lists; /* array of queue override mapping lists */
0210     bool port_mtu_change_allowed;
0211     struct {
0212         unsigned int count;
0213         unsigned int interval; /* in ms */
0214         atomic_t count_pending;
0215         struct delayed_work dw;
0216     } notify_peers;
0217     struct {
0218         unsigned int count;
0219         unsigned int interval; /* in ms */
0220         atomic_t count_pending;
0221         struct delayed_work dw;
0222     } mcast_rejoin;
0223     struct lock_class_key team_lock_key;
0224     long mode_priv[TEAM_MODE_PRIV_LONGS];
0225 };
0226 
0227 static inline int team_dev_queue_xmit(struct team *team, struct team_port *port,
0228                       struct sk_buff *skb)
0229 {
0230     BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
0231              sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
0232     skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
0233 
0234     skb->dev = port->dev;
0235     if (unlikely(netpoll_tx_running(team->dev))) {
0236         team_netpoll_send_skb(port, skb);
0237         return 0;
0238     }
0239     return dev_queue_xmit(skb);
0240 }
0241 
0242 static inline struct hlist_head *team_port_index_hash(struct team *team,
0243                               int port_index)
0244 {
0245     return &team->en_port_hlist[port_index & (TEAM_PORT_HASHENTRIES - 1)];
0246 }
0247 
0248 static inline struct team_port *team_get_port_by_index(struct team *team,
0249                                int port_index)
0250 {
0251     struct team_port *port;
0252     struct hlist_head *head = team_port_index_hash(team, port_index);
0253 
0254     hlist_for_each_entry(port, head, hlist)
0255         if (port->index == port_index)
0256             return port;
0257     return NULL;
0258 }
0259 
0260 static inline int team_num_to_port_index(struct team *team, unsigned int num)
0261 {
0262     int en_port_count = READ_ONCE(team->en_port_count);
0263 
0264     if (unlikely(!en_port_count))
0265         return 0;
0266     return num % en_port_count;
0267 }
0268 
0269 static inline struct team_port *team_get_port_by_index_rcu(struct team *team,
0270                                int port_index)
0271 {
0272     struct team_port *port;
0273     struct hlist_head *head = team_port_index_hash(team, port_index);
0274 
0275     hlist_for_each_entry_rcu(port, head, hlist)
0276         if (port->index == port_index)
0277             return port;
0278     return NULL;
0279 }
0280 
0281 static inline struct team_port *
0282 team_get_first_port_txable_rcu(struct team *team, struct team_port *port)
0283 {
0284     struct team_port *cur;
0285 
0286     if (likely(team_port_txable(port)))
0287         return port;
0288     cur = port;
0289     list_for_each_entry_continue_rcu(cur, &team->port_list, list)
0290         if (team_port_txable(cur))
0291             return cur;
0292     list_for_each_entry_rcu(cur, &team->port_list, list) {
0293         if (cur == port)
0294             break;
0295         if (team_port_txable(cur))
0296             return cur;
0297     }
0298     return NULL;
0299 }
0300 
0301 extern int team_options_register(struct team *team,
0302                  const struct team_option *option,
0303                  size_t option_count);
0304 extern void team_options_unregister(struct team *team,
0305                     const struct team_option *option,
0306                     size_t option_count);
0307 extern int team_mode_register(const struct team_mode *mode);
0308 extern void team_mode_unregister(const struct team_mode *mode);
0309 
0310 #define TEAM_DEFAULT_NUM_TX_QUEUES 16
0311 #define TEAM_DEFAULT_NUM_RX_QUEUES 16
0312 
0313 #define MODULE_ALIAS_TEAM_MODE(kind) MODULE_ALIAS("team-mode-" kind)
0314 
0315 #endif /* _LINUX_IF_TEAM_H_ */