18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef _NET_BOND_ALB_H
78c2ecf20Sopenharmony_ci#define _NET_BOND_ALB_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistruct bonding;
128c2ecf20Sopenharmony_cistruct slave;
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define BOND_ALB_INFO(bond)   ((bond)->alb_info)
158c2ecf20Sopenharmony_ci#define SLAVE_TLB_INFO(slave) ((slave)->tlb_info)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define ALB_TIMER_TICKS_PER_SEC	    10	/* should be a divisor of HZ */
188c2ecf20Sopenharmony_ci#define BOND_TLB_REBALANCE_INTERVAL 10	/* In seconds, periodic re-balancing.
198c2ecf20Sopenharmony_ci					 * Used for division - never set
208c2ecf20Sopenharmony_ci					 * to zero !!!
218c2ecf20Sopenharmony_ci					 */
228c2ecf20Sopenharmony_ci#define BOND_ALB_DEFAULT_LP_INTERVAL 1
238c2ecf20Sopenharmony_ci#define BOND_ALB_LP_INTERVAL(bond) (bond->params.lp_interval)	/* In seconds, periodic send of
248c2ecf20Sopenharmony_ci								 * learning packets to the switch
258c2ecf20Sopenharmony_ci								 */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
288c2ecf20Sopenharmony_ci				  * ALB_TIMER_TICKS_PER_SEC)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define BOND_ALB_LP_TICKS(bond) (BOND_ALB_LP_INTERVAL(bond) \
318c2ecf20Sopenharmony_ci			   * ALB_TIMER_TICKS_PER_SEC)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define TLB_HASH_TABLE_SIZE 256	/* The size of the clients hash table.
348c2ecf20Sopenharmony_ci				 * Note that this value MUST NOT be smaller
358c2ecf20Sopenharmony_ci				 * because the key hash table is BYTE wide !
368c2ecf20Sopenharmony_ci				 */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define TLB_NULL_INDEX		0xffffffff
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* rlb defs */
428c2ecf20Sopenharmony_ci#define RLB_HASH_TABLE_SIZE	256
438c2ecf20Sopenharmony_ci#define RLB_NULL_INDEX		0xffffffff
448c2ecf20Sopenharmony_ci#define RLB_UPDATE_DELAY	(2*ALB_TIMER_TICKS_PER_SEC) /* 2 seconds */
458c2ecf20Sopenharmony_ci#define RLB_ARP_BURST_SIZE	2
468c2ecf20Sopenharmony_ci#define RLB_UPDATE_RETRY	3 /* 3-ticks - must be smaller than the rlb
478c2ecf20Sopenharmony_ci				   * rebalance interval (5 min).
488c2ecf20Sopenharmony_ci				   */
498c2ecf20Sopenharmony_ci/* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
508c2ecf20Sopenharmony_ci * promiscuous after failover
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_ci#define RLB_PROMISC_TIMEOUT	(10*ALB_TIMER_TICKS_PER_SEC)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct tlb_client_info {
568c2ecf20Sopenharmony_ci	struct slave *tx_slave;	/* A pointer to slave used for transmiting
578c2ecf20Sopenharmony_ci				 * packets to a Client that the Hash function
588c2ecf20Sopenharmony_ci				 * gave this entry index.
598c2ecf20Sopenharmony_ci				 */
608c2ecf20Sopenharmony_ci	u32 tx_bytes;		/* Each Client accumulates the BytesTx that
618c2ecf20Sopenharmony_ci				 * were transmitted to it, and after each
628c2ecf20Sopenharmony_ci				 * CallBack the LoadHistory is divided
638c2ecf20Sopenharmony_ci				 * by the balance interval
648c2ecf20Sopenharmony_ci				 */
658c2ecf20Sopenharmony_ci	u32 load_history;	/* This field contains the amount of Bytes
668c2ecf20Sopenharmony_ci				 * that were transmitted to this client by
678c2ecf20Sopenharmony_ci				 * the server on the previous balance
688c2ecf20Sopenharmony_ci				 * interval in Bps.
698c2ecf20Sopenharmony_ci				 */
708c2ecf20Sopenharmony_ci	u32 next;		/* The next Hash table entry index, assigned
718c2ecf20Sopenharmony_ci				 * to use the same adapter for transmit.
728c2ecf20Sopenharmony_ci				 */
738c2ecf20Sopenharmony_ci	u32 prev;		/* The previous Hash table entry index,
748c2ecf20Sopenharmony_ci				 * assigned to use the same
758c2ecf20Sopenharmony_ci				 */
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* -------------------------------------------------------------------------
798c2ecf20Sopenharmony_ci * struct rlb_client_info contains all info related to a specific rx client
808c2ecf20Sopenharmony_ci * connection. This is the Clients Hash Table entry struct.
818c2ecf20Sopenharmony_ci * Note that this is not a proper hash table; if a new client's IP address
828c2ecf20Sopenharmony_ci * hash collides with an existing client entry, the old entry is replaced.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * There is a linked list (linked by the used_next and used_prev members)
858c2ecf20Sopenharmony_ci * linking all the used entries of the hash table. This allows updating
868c2ecf20Sopenharmony_ci * all the clients without walking over all the unused elements of the table.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * There are also linked lists of entries with identical hash(ip_src). These
898c2ecf20Sopenharmony_ci * allow cleaning up the table from ip_src<->mac_src associations that have
908c2ecf20Sopenharmony_ci * become outdated and would cause sending out invalid ARP updates to the
918c2ecf20Sopenharmony_ci * network. These are linked by the (src_next and src_prev members).
928c2ecf20Sopenharmony_ci * -------------------------------------------------------------------------
938c2ecf20Sopenharmony_ci */
948c2ecf20Sopenharmony_cistruct rlb_client_info {
958c2ecf20Sopenharmony_ci	__be32 ip_src;		/* the server IP address */
968c2ecf20Sopenharmony_ci	__be32 ip_dst;		/* the client IP address */
978c2ecf20Sopenharmony_ci	u8  mac_src[ETH_ALEN];	/* the server MAC address */
988c2ecf20Sopenharmony_ci	u8  mac_dst[ETH_ALEN];	/* the client MAC address */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* list of used hash table entries, starting at rx_hashtbl_used_head */
1018c2ecf20Sopenharmony_ci	u32 used_next;
1028c2ecf20Sopenharmony_ci	u32 used_prev;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* ip_src based hashing */
1058c2ecf20Sopenharmony_ci	u32 src_next;	/* next entry with same hash(ip_src) */
1068c2ecf20Sopenharmony_ci	u32 src_prev;	/* prev entry with same hash(ip_src) */
1078c2ecf20Sopenharmony_ci	u32 src_first;	/* first entry with hash(ip_src) == this entry's index */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	u8  assigned;		/* checking whether this entry is assigned */
1108c2ecf20Sopenharmony_ci	u8  ntt;		/* flag - need to transmit client info */
1118c2ecf20Sopenharmony_ci	struct slave *slave;	/* the slave assigned to this client */
1128c2ecf20Sopenharmony_ci	unsigned short vlan_id;	/* VLAN tag associated with IP address */
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistruct tlb_slave_info {
1168c2ecf20Sopenharmony_ci	u32 head;	/* Index to the head of the bi-directional clients
1178c2ecf20Sopenharmony_ci			 * hash table entries list. The entries in the list
1188c2ecf20Sopenharmony_ci			 * are the entries that were assigned to use this
1198c2ecf20Sopenharmony_ci			 * slave for transmit.
1208c2ecf20Sopenharmony_ci			 */
1218c2ecf20Sopenharmony_ci	u32 load;	/* Each slave sums the loadHistory of all clients
1228c2ecf20Sopenharmony_ci			 * assigned to it
1238c2ecf20Sopenharmony_ci			 */
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistruct alb_bond_info {
1278c2ecf20Sopenharmony_ci	struct tlb_client_info	*tx_hashtbl; /* Dynamically allocated */
1288c2ecf20Sopenharmony_ci	u32			unbalanced_load;
1298c2ecf20Sopenharmony_ci	atomic_t		tx_rebalance_counter;
1308c2ecf20Sopenharmony_ci	int			lp_counter;
1318c2ecf20Sopenharmony_ci	/* -------- rlb parameters -------- */
1328c2ecf20Sopenharmony_ci	int rlb_enabled;
1338c2ecf20Sopenharmony_ci	struct rlb_client_info	*rx_hashtbl;	/* Receive hash table */
1348c2ecf20Sopenharmony_ci	u32			rx_hashtbl_used_head;
1358c2ecf20Sopenharmony_ci	u8			rx_ntt;	/* flag - need to transmit
1368c2ecf20Sopenharmony_ci					 * to all rx clients
1378c2ecf20Sopenharmony_ci					 */
1388c2ecf20Sopenharmony_ci	struct slave		*rx_slave;/* last slave to xmit from */
1398c2ecf20Sopenharmony_ci	u8			primary_is_promisc;	   /* boolean */
1408c2ecf20Sopenharmony_ci	u32			rlb_promisc_timeout_counter;/* counts primary
1418c2ecf20Sopenharmony_ci							     * promiscuity time
1428c2ecf20Sopenharmony_ci							     */
1438c2ecf20Sopenharmony_ci	u32			rlb_update_delay_counter;
1448c2ecf20Sopenharmony_ci	u32			rlb_update_retry_counter;/* counter of retries
1458c2ecf20Sopenharmony_ci							  * of client update
1468c2ecf20Sopenharmony_ci							  */
1478c2ecf20Sopenharmony_ci	u8			rlb_rebalance;	/* flag - indicates that the
1488c2ecf20Sopenharmony_ci						 * rx traffic should be
1498c2ecf20Sopenharmony_ci						 * rebalanced
1508c2ecf20Sopenharmony_ci						 */
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ciint bond_alb_initialize(struct bonding *bond, int rlb_enabled);
1548c2ecf20Sopenharmony_civoid bond_alb_deinitialize(struct bonding *bond);
1558c2ecf20Sopenharmony_ciint bond_alb_init_slave(struct bonding *bond, struct slave *slave);
1568c2ecf20Sopenharmony_civoid bond_alb_deinit_slave(struct bonding *bond, struct slave *slave);
1578c2ecf20Sopenharmony_civoid bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link);
1588c2ecf20Sopenharmony_civoid bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave);
1598c2ecf20Sopenharmony_cinetdev_tx_t bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev);
1608c2ecf20Sopenharmony_cinetdev_tx_t bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev);
1618c2ecf20Sopenharmony_cistruct slave *bond_xmit_alb_slave_get(struct bonding *bond,
1628c2ecf20Sopenharmony_ci				      struct sk_buff *skb);
1638c2ecf20Sopenharmony_cistruct slave *bond_xmit_tlb_slave_get(struct bonding *bond,
1648c2ecf20Sopenharmony_ci				      struct sk_buff *skb);
1658c2ecf20Sopenharmony_civoid bond_alb_monitor(struct work_struct *);
1668c2ecf20Sopenharmony_ciint bond_alb_set_mac_address(struct net_device *bond_dev, void *addr);
1678c2ecf20Sopenharmony_civoid bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id);
1688c2ecf20Sopenharmony_ci#endif /* _NET_BOND_ALB_H */
1698c2ecf20Sopenharmony_ci
170