Commit 46a275a5 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'net-marvell-prestera-add-nexthop-routes-offloading'

Yevhen Orlov says:

====================
net: marvell: prestera: add nexthop routes offloading

Add support for nexthop routes for Marvell Prestera driver.
Subscribe on NEIGH_UPDATE events.

Add features:
 - Support connected route adding
   e.g.: "ip address add 1.1.1.1/24 dev sw1p1"
   e.g.: "ip route add 6.6.6/24 dev sw1p1"
 - Support nexthop route adding
   e.g.: "ip route add 5.5.5/24 via 1.1.1.2"
 - Support ECMP route adding
   e.g.: "ip route add 5.5.5/24 nexthop via 1.1.1.2 nexthop via 1.1.1.3"
 - Support "offload" and "trap" flags per each nexthop
 - Support "offload" flag for neighbours

Limitations:
 - Only "local" and "main" tables supported
 - Only generic interfaces supported for router (no bridges or vlans)

Flags meaning:
  ip route add 5.5.5/24 nexthop via 2.2.2.2 nexthop via 2.2.2.3
  ip route show
  ...
  5.5.5.0/24 rt_offload
        nexthop via 2.2.2.2 dev sw1p31 weight 1 trap
        nexthop via 2.2.2.3 dev sw1p31 weight 1 trap
  ...
  # When you just add route - lpm entry became occupied
  # in HW ("rt_offload" flag), but related to nexthops neighbours
  # still not resolved ("trap" flag).
  #
  # After some time...
  ip route show
  ...
  5.5.5.0/24 rt_offload
        nexthop via 2.2.2.2 dev sw1p31 weight 1 offload
        nexthop via 2.2.2.3 dev sw1p31 weight 1 offload
  ...
  # You will see, that appropriate neighbours was resolved and nexthop
  # entries occupied in HW too ("offload" flag)
Co-developed-by: default avatarTaras Chornyi <tchornyi@marvell.com>
Signed-off-by: default avatarTaras Chornyi <tchornyi@marvell.com>
Co-developed-by: default avatarOleksandr Mazur <oleksandr.mazur@plvision.eu>
Signed-off-by: default avatarOleksandr Mazur <oleksandr.mazur@plvision.eu>
Signed-off-by: default avatarYevhen Orlov <yevhen.orlov@plvision.eu>

Changes for v2:
* Add more reviewers in CC
* Check if route nexthop or direct with fib_nh_gw_family instead of fib_nh_scope
  This is needed after,
  747c1430 ("ip: fix dflt addr selection for connected nexthop"),
  because direct route is now with the same scope as nexthop (RT_SCOPE_LINK)

Changes for v3:
* Resolve "unused functions" warnings, after
  patch ("net: marvell: prestera: Add heplers to interact ... "), and before
  patch ("net: marvell: prestera: Add neighbour cache accounting")

Changes for v4:
* Rebase to the latest master to resolve patch applying issues

Changes for v5:
* Repack structures to prevent holes
* Remove unused variables
* Fix misspeling issues

Changes for v6:
* Rebase on top of master
* Fix smatch warnings

Changes for v7:
* Rebase on top of master
* Refactor: use "fib_lookup" instead of "fib_new_table"+"fib_table_lookup",
  according to Paolo Abeni suggestion
* Refactor: use "rhashtable_free_and_destroy" instead of rhashtable
  walk, according to Paolo Abeni suggestion
====================

Link: https://lore.kernel.org/r/20221001093417.22388-1-yevhen.orlov@plvision.euSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 899b8cd0 ae15ed6e
...@@ -306,17 +306,27 @@ struct prestera_switch { ...@@ -306,17 +306,27 @@ struct prestera_switch {
struct prestera_counter *counter; struct prestera_counter *counter;
u8 lag_member_max; u8 lag_member_max;
u8 lag_max; u8 lag_max;
u32 size_tbl_router_nexthop;
}; };
struct prestera_router { struct prestera_router {
struct prestera_switch *sw; struct prestera_switch *sw;
struct list_head vr_list; struct list_head vr_list;
struct list_head rif_entry_list; struct list_head rif_entry_list;
struct rhashtable nh_neigh_ht;
struct rhashtable nexthop_group_ht;
struct rhashtable fib_ht; struct rhashtable fib_ht;
struct rhashtable kern_neigh_cache_ht;
struct rhashtable kern_fib_cache_ht; struct rhashtable kern_fib_cache_ht;
struct notifier_block inetaddr_nb; struct notifier_block inetaddr_nb;
struct notifier_block inetaddr_valid_nb; struct notifier_block inetaddr_valid_nb;
struct notifier_block fib_nb; struct notifier_block fib_nb;
struct notifier_block netevent_nb;
u8 *nhgrp_hw_state_cache; /* Bitmap cached hw state of nhs */
unsigned long nhgrp_hw_cache_kick; /* jiffies */
struct {
struct delayed_work dw;
} neighs_update;
}; };
struct prestera_rxtx_params { struct prestera_rxtx_params {
...@@ -362,6 +372,8 @@ int prestera_port_cfg_mac_write(struct prestera_port *port, ...@@ -362,6 +372,8 @@ int prestera_port_cfg_mac_write(struct prestera_port *port,
struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev); struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev);
void prestera_queue_work(struct work_struct *work); void prestera_queue_work(struct work_struct *work);
void prestera_queue_delayed_work(struct delayed_work *work, unsigned long delay);
void prestera_queue_drain(void);
int prestera_port_learning_set(struct prestera_port *port, bool learn_enable); int prestera_port_learning_set(struct prestera_port *port, bool learn_enable);
int prestera_port_uc_flood_set(struct prestera_port *port, bool flood); int prestera_port_uc_flood_set(struct prestera_port *port, bool flood);
......
...@@ -10,11 +10,14 @@ ...@@ -10,11 +10,14 @@
#include "prestera_hw.h" #include "prestera_hw.h"
#include "prestera_acl.h" #include "prestera_acl.h"
#include "prestera_counter.h" #include "prestera_counter.h"
#include "prestera_router_hw.h"
#define PRESTERA_SWITCH_INIT_TIMEOUT_MS (30 * 1000) #define PRESTERA_SWITCH_INIT_TIMEOUT_MS (30 * 1000)
#define PRESTERA_MIN_MTU 64 #define PRESTERA_MIN_MTU 64
#define PRESTERA_MSG_CHUNK_SIZE 1024
enum prestera_cmd_type_t { enum prestera_cmd_type_t {
PRESTERA_CMD_TYPE_SWITCH_INIT = 0x1, PRESTERA_CMD_TYPE_SWITCH_INIT = 0x1,
PRESTERA_CMD_TYPE_SWITCH_ATTR_SET = 0x2, PRESTERA_CMD_TYPE_SWITCH_ATTR_SET = 0x2,
...@@ -57,6 +60,10 @@ enum prestera_cmd_type_t { ...@@ -57,6 +60,10 @@ enum prestera_cmd_type_t {
PRESTERA_CMD_TYPE_ROUTER_RIF_DELETE = 0x601, PRESTERA_CMD_TYPE_ROUTER_RIF_DELETE = 0x601,
PRESTERA_CMD_TYPE_ROUTER_LPM_ADD = 0x610, PRESTERA_CMD_TYPE_ROUTER_LPM_ADD = 0x610,
PRESTERA_CMD_TYPE_ROUTER_LPM_DELETE = 0x611, PRESTERA_CMD_TYPE_ROUTER_LPM_DELETE = 0x611,
PRESTERA_CMD_TYPE_ROUTER_NH_GRP_SET = 0x622,
PRESTERA_CMD_TYPE_ROUTER_NH_GRP_BLK_GET = 0x645,
PRESTERA_CMD_TYPE_ROUTER_NH_GRP_ADD = 0x623,
PRESTERA_CMD_TYPE_ROUTER_NH_GRP_DELETE = 0x624,
PRESTERA_CMD_TYPE_ROUTER_VR_CREATE = 0x630, PRESTERA_CMD_TYPE_ROUTER_VR_CREATE = 0x630,
PRESTERA_CMD_TYPE_ROUTER_VR_DELETE = 0x631, PRESTERA_CMD_TYPE_ROUTER_VR_DELETE = 0x631,
...@@ -542,6 +549,14 @@ struct prestera_msg_ip_addr { ...@@ -542,6 +549,14 @@ struct prestera_msg_ip_addr {
u8 __pad[3]; u8 __pad[3];
}; };
struct prestera_msg_nh {
struct prestera_msg_iface oif;
__le32 hw_id;
u8 mac[ETH_ALEN];
u8 is_active;
u8 pad;
};
struct prestera_msg_rif_req { struct prestera_msg_rif_req {
struct prestera_msg_cmd cmd; struct prestera_msg_cmd cmd;
struct prestera_msg_iface iif; struct prestera_msg_iface iif;
...@@ -567,6 +582,34 @@ struct prestera_msg_lpm_req { ...@@ -567,6 +582,34 @@ struct prestera_msg_lpm_req {
u8 __pad[2]; u8 __pad[2];
}; };
struct prestera_msg_nh_req {
struct prestera_msg_cmd cmd;
struct prestera_msg_nh nh[PRESTERA_NHGR_SIZE_MAX];
__le32 size;
__le32 grp_id;
};
struct prestera_msg_nh_chunk_req {
struct prestera_msg_cmd cmd;
__le32 offset;
};
struct prestera_msg_nh_chunk_resp {
struct prestera_msg_ret ret;
u8 hw_state[PRESTERA_MSG_CHUNK_SIZE];
};
struct prestera_msg_nh_grp_req {
struct prestera_msg_cmd cmd;
__le32 grp_id;
__le32 size;
};
struct prestera_msg_nh_grp_resp {
struct prestera_msg_ret ret;
__le32 grp_id;
};
struct prestera_msg_vr_req { struct prestera_msg_vr_req {
struct prestera_msg_cmd cmd; struct prestera_msg_cmd cmd;
__le16 vr_id; __le16 vr_id;
...@@ -729,11 +772,15 @@ static void prestera_hw_build_tests(void) ...@@ -729,11 +772,15 @@ static void prestera_hw_build_tests(void)
BUILD_BUG_ON(sizeof(struct prestera_msg_flood_domain_ports_reset_req) != 8); BUILD_BUG_ON(sizeof(struct prestera_msg_flood_domain_ports_reset_req) != 8);
BUILD_BUG_ON(sizeof(struct prestera_msg_mdb_create_req) != 16); BUILD_BUG_ON(sizeof(struct prestera_msg_mdb_create_req) != 16);
BUILD_BUG_ON(sizeof(struct prestera_msg_mdb_destroy_req) != 16); BUILD_BUG_ON(sizeof(struct prestera_msg_mdb_destroy_req) != 16);
BUILD_BUG_ON(sizeof(struct prestera_msg_nh_req) != 124);
BUILD_BUG_ON(sizeof(struct prestera_msg_nh_chunk_req) != 8);
BUILD_BUG_ON(sizeof(struct prestera_msg_nh_grp_req) != 12);
/* structure that are part of req/resp fw messages */ /* structure that are part of req/resp fw messages */
BUILD_BUG_ON(sizeof(struct prestera_msg_iface) != 16); BUILD_BUG_ON(sizeof(struct prestera_msg_iface) != 16);
BUILD_BUG_ON(sizeof(struct prestera_msg_ip_addr) != 20); BUILD_BUG_ON(sizeof(struct prestera_msg_ip_addr) != 20);
BUILD_BUG_ON(sizeof(struct prestera_msg_flood_domain_port) != 12); BUILD_BUG_ON(sizeof(struct prestera_msg_flood_domain_port) != 12);
BUILD_BUG_ON(sizeof(struct prestera_msg_nh) != 28);
/* check responses */ /* check responses */
BUILD_BUG_ON(sizeof(struct prestera_msg_common_resp) != 8); BUILD_BUG_ON(sizeof(struct prestera_msg_common_resp) != 8);
...@@ -750,6 +797,8 @@ static void prestera_hw_build_tests(void) ...@@ -750,6 +797,8 @@ static void prestera_hw_build_tests(void)
BUILD_BUG_ON(sizeof(struct prestera_msg_vr_resp) != 12); BUILD_BUG_ON(sizeof(struct prestera_msg_vr_resp) != 12);
BUILD_BUG_ON(sizeof(struct prestera_msg_policer_resp) != 12); BUILD_BUG_ON(sizeof(struct prestera_msg_policer_resp) != 12);
BUILD_BUG_ON(sizeof(struct prestera_msg_flood_domain_create_resp) != 12); BUILD_BUG_ON(sizeof(struct prestera_msg_flood_domain_create_resp) != 12);
BUILD_BUG_ON(sizeof(struct prestera_msg_nh_chunk_resp) != 1032);
BUILD_BUG_ON(sizeof(struct prestera_msg_nh_grp_resp) != 12);
/* check events */ /* check events */
BUILD_BUG_ON(sizeof(struct prestera_msg_event_port) != 20); BUILD_BUG_ON(sizeof(struct prestera_msg_event_port) != 20);
...@@ -1027,6 +1076,8 @@ int prestera_hw_switch_init(struct prestera_switch *sw) ...@@ -1027,6 +1076,8 @@ int prestera_hw_switch_init(struct prestera_switch *sw)
sw->id = resp.switch_id; sw->id = resp.switch_id;
sw->lag_member_max = resp.lag_member_max; sw->lag_member_max = resp.lag_member_max;
sw->lag_max = resp.lag_max; sw->lag_max = resp.lag_max;
sw->size_tbl_router_nexthop =
__le32_to_cpu(resp.size_tbl_router_nexthop);
return 0; return 0;
} }
...@@ -2037,6 +2088,85 @@ int prestera_hw_lpm_del(struct prestera_switch *sw, u16 vr_id, ...@@ -2037,6 +2088,85 @@ int prestera_hw_lpm_del(struct prestera_switch *sw, u16 vr_id,
sizeof(req)); sizeof(req));
} }
int prestera_hw_nh_entries_set(struct prestera_switch *sw, int count,
struct prestera_neigh_info *nhs, u32 grp_id)
{
struct prestera_msg_nh_req req = { .size = __cpu_to_le32((u32)count),
.grp_id = __cpu_to_le32(grp_id) };
int i, err;
for (i = 0; i < count; i++) {
req.nh[i].is_active = nhs[i].connected;
memcpy(&req.nh[i].mac, nhs[i].ha, ETH_ALEN);
err = prestera_iface_to_msg(&nhs[i].iface, &req.nh[i].oif);
if (err)
return err;
}
return prestera_cmd(sw, PRESTERA_CMD_TYPE_ROUTER_NH_GRP_SET, &req.cmd,
sizeof(req));
}
int prestera_hw_nhgrp_blk_get(struct prestera_switch *sw,
u8 *hw_state, u32 buf_size /* Buffer in bytes */)
{
static struct prestera_msg_nh_chunk_resp resp;
struct prestera_msg_nh_chunk_req req;
u32 buf_offset;
int err;
memset(&hw_state[0], 0, buf_size);
buf_offset = 0;
while (1) {
if (buf_offset >= buf_size)
break;
memset(&req, 0, sizeof(req));
req.offset = __cpu_to_le32(buf_offset * 8); /* 8 bits in u8 */
err = prestera_cmd_ret(sw,
PRESTERA_CMD_TYPE_ROUTER_NH_GRP_BLK_GET,
&req.cmd, sizeof(req), &resp.ret,
sizeof(resp));
if (err)
return err;
memcpy(&hw_state[buf_offset], &resp.hw_state[0],
buf_offset + PRESTERA_MSG_CHUNK_SIZE > buf_size ?
buf_size - buf_offset : PRESTERA_MSG_CHUNK_SIZE);
buf_offset += PRESTERA_MSG_CHUNK_SIZE;
}
return 0;
}
int prestera_hw_nh_group_create(struct prestera_switch *sw, u16 nh_count,
u32 *grp_id)
{
struct prestera_msg_nh_grp_req req = { .size = __cpu_to_le32((u32)nh_count) };
struct prestera_msg_nh_grp_resp resp;
int err;
err = prestera_cmd_ret(sw, PRESTERA_CMD_TYPE_ROUTER_NH_GRP_ADD,
&req.cmd, sizeof(req), &resp.ret, sizeof(resp));
if (err)
return err;
*grp_id = __le32_to_cpu(resp.grp_id);
return err;
}
int prestera_hw_nh_group_delete(struct prestera_switch *sw, u16 nh_count,
u32 grp_id)
{
struct prestera_msg_nh_grp_req req = {
.grp_id = __cpu_to_le32(grp_id),
.size = __cpu_to_le32(nh_count)
};
return prestera_cmd(sw, PRESTERA_CMD_TYPE_ROUTER_NH_GRP_DELETE,
&req.cmd, sizeof(req));
}
int prestera_hw_rxtx_init(struct prestera_switch *sw, int prestera_hw_rxtx_init(struct prestera_switch *sw,
struct prestera_rxtx_params *params) struct prestera_rxtx_params *params)
{ {
......
...@@ -146,6 +146,7 @@ struct prestera_counter_stats; ...@@ -146,6 +146,7 @@ struct prestera_counter_stats;
struct prestera_iface; struct prestera_iface;
struct prestera_flood_domain; struct prestera_flood_domain;
struct prestera_mdb_entry; struct prestera_mdb_entry;
struct prestera_neigh_info;
/* Switch API */ /* Switch API */
int prestera_hw_switch_init(struct prestera_switch *sw); int prestera_hw_switch_init(struct prestera_switch *sw);
...@@ -266,6 +267,16 @@ int prestera_hw_lpm_add(struct prestera_switch *sw, u16 vr_id, ...@@ -266,6 +267,16 @@ int prestera_hw_lpm_add(struct prestera_switch *sw, u16 vr_id,
int prestera_hw_lpm_del(struct prestera_switch *sw, u16 vr_id, int prestera_hw_lpm_del(struct prestera_switch *sw, u16 vr_id,
__be32 dst, u32 dst_len); __be32 dst, u32 dst_len);
/* NH API */
int prestera_hw_nh_entries_set(struct prestera_switch *sw, int count,
struct prestera_neigh_info *nhs, u32 grp_id);
int prestera_hw_nhgrp_blk_get(struct prestera_switch *sw,
u8 *hw_state, u32 buf_size /* Buffer in bytes */);
int prestera_hw_nh_group_create(struct prestera_switch *sw, u16 nh_count,
u32 *grp_id);
int prestera_hw_nh_group_delete(struct prestera_switch *sw, u16 nh_count,
u32 grp_id);
/* Event handlers */ /* Event handlers */
int prestera_hw_event_handler_register(struct prestera_switch *sw, int prestera_hw_event_handler_register(struct prestera_switch *sw,
enum prestera_event_type type, enum prestera_event_type type,
......
...@@ -36,6 +36,17 @@ void prestera_queue_work(struct work_struct *work) ...@@ -36,6 +36,17 @@ void prestera_queue_work(struct work_struct *work)
queue_work(prestera_owq, work); queue_work(prestera_owq, work);
} }
void prestera_queue_delayed_work(struct delayed_work *work, unsigned long delay)
{
queue_delayed_work(prestera_wq, work, delay);
}
void prestera_queue_drain(void)
{
drain_workqueue(prestera_wq);
drain_workqueue(prestera_owq);
}
int prestera_port_learning_set(struct prestera_port *port, bool learn) int prestera_port_learning_set(struct prestera_port *port, bool learn)
{ {
return prestera_hw_port_learning_set(port, learn); return prestera_hw_port_learning_set(port, learn);
......
...@@ -7,10 +7,35 @@ ...@@ -7,10 +7,35 @@
#include <net/inet_dscp.h> #include <net/inet_dscp.h>
#include <net/switchdev.h> #include <net/switchdev.h>
#include <linux/rhashtable.h> #include <linux/rhashtable.h>
#include <net/nexthop.h>
#include <net/arp.h>
#include <linux/if_vlan.h>
#include <linux/if_macvlan.h>
#include <net/netevent.h>
#include "prestera.h" #include "prestera.h"
#include "prestera_router_hw.h" #include "prestera_router_hw.h"
#define PRESTERA_IMPLICITY_RESOLVE_DEAD_NEIGH
#define PRESTERA_NH_PROBE_INTERVAL 5000 /* ms */
struct prestera_kern_neigh_cache_key {
struct prestera_ip_addr addr;
struct net_device *dev;
};
struct prestera_kern_neigh_cache {
struct prestera_kern_neigh_cache_key key;
struct rhash_head ht_node;
struct list_head kern_fib_cache_list;
/* Hold prepared nh_neigh info if is in_kernel */
struct prestera_neigh_info nh_neigh_info;
/* Indicate if neighbour is reachable by direct route */
bool reachable;
/* Lock cache if neigh is present in kernel */
bool in_kernel;
};
struct prestera_kern_fib_cache_key { struct prestera_kern_fib_cache_key {
struct prestera_ip_addr addr; struct prestera_ip_addr addr;
u32 prefix_len; u32 prefix_len;
...@@ -23,15 +48,29 @@ struct prestera_kern_fib_cache { ...@@ -23,15 +48,29 @@ struct prestera_kern_fib_cache {
struct { struct {
struct prestera_fib_key fib_key; struct prestera_fib_key fib_key;
enum prestera_fib_type fib_type; enum prestera_fib_type fib_type;
struct prestera_nexthop_group_key nh_grp_key;
} lpm_info; /* hold prepared lpm info */ } lpm_info; /* hold prepared lpm info */
/* Indicate if route is not overlapped by another table */ /* Indicate if route is not overlapped by another table */
struct rhash_head ht_node; /* node of prestera_router */ struct rhash_head ht_node; /* node of prestera_router */
struct fib_info *fi; struct prestera_kern_neigh_cache_head {
dscp_t kern_dscp; struct prestera_kern_fib_cache *this;
u8 kern_type; struct list_head head;
struct prestera_kern_neigh_cache *n_cache;
} kern_neigh_cache_head[PRESTERA_NHGR_SIZE_MAX];
union {
struct fib_notifier_info info; /* point to any of 4/6 */
struct fib_entry_notifier_info fen4_info;
};
bool reachable; bool reachable;
}; };
static const struct rhashtable_params __prestera_kern_neigh_cache_ht_params = {
.key_offset = offsetof(struct prestera_kern_neigh_cache, key),
.head_offset = offsetof(struct prestera_kern_neigh_cache, ht_node),
.key_len = sizeof(struct prestera_kern_neigh_cache_key),
.automatic_shrinking = true,
};
static const struct rhashtable_params __prestera_kern_fib_cache_ht_params = { static const struct rhashtable_params __prestera_kern_fib_cache_ht_params = {
.key_offset = offsetof(struct prestera_kern_fib_cache, key), .key_offset = offsetof(struct prestera_kern_fib_cache, key),
.head_offset = offsetof(struct prestera_kern_fib_cache, ht_node), .head_offset = offsetof(struct prestera_kern_fib_cache, ht_node),
...@@ -51,15 +90,450 @@ static u32 prestera_fix_tb_id(u32 tb_id) ...@@ -51,15 +90,450 @@ static u32 prestera_fix_tb_id(u32 tb_id)
} }
static void static void
prestera_util_fen_info2fib_cache_key(struct fib_entry_notifier_info *fen_info, prestera_util_fen_info2fib_cache_key(struct fib_notifier_info *info,
struct prestera_kern_fib_cache_key *key) struct prestera_kern_fib_cache_key *key)
{ {
struct fib_entry_notifier_info *fen_info =
container_of(info, struct fib_entry_notifier_info, info);
memset(key, 0, sizeof(*key)); memset(key, 0, sizeof(*key));
key->addr.v = PRESTERA_IPV4;
key->addr.u.ipv4 = cpu_to_be32(fen_info->dst); key->addr.u.ipv4 = cpu_to_be32(fen_info->dst);
key->prefix_len = fen_info->dst_len; key->prefix_len = fen_info->dst_len;
key->kern_tb_id = fen_info->tb_id; key->kern_tb_id = fen_info->tb_id;
} }
static int prestera_util_nhc2nc_key(struct prestera_switch *sw,
struct fib_nh_common *nhc,
struct prestera_kern_neigh_cache_key *nk)
{
memset(nk, 0, sizeof(*nk));
if (nhc->nhc_gw_family == AF_INET) {
nk->addr.v = PRESTERA_IPV4;
nk->addr.u.ipv4 = nhc->nhc_gw.ipv4;
} else {
nk->addr.v = PRESTERA_IPV6;
nk->addr.u.ipv6 = nhc->nhc_gw.ipv6;
}
nk->dev = nhc->nhc_dev;
return 0;
}
static void
prestera_util_nc_key2nh_key(struct prestera_kern_neigh_cache_key *ck,
struct prestera_nh_neigh_key *nk)
{
memset(nk, 0, sizeof(*nk));
nk->addr = ck->addr;
nk->rif = (void *)ck->dev;
}
static bool
prestera_util_nhc_eq_n_cache_key(struct prestera_switch *sw,
struct fib_nh_common *nhc,
struct prestera_kern_neigh_cache_key *nk)
{
struct prestera_kern_neigh_cache_key tk;
int err;
err = prestera_util_nhc2nc_key(sw, nhc, &tk);
if (err)
return false;
if (memcmp(&tk, nk, sizeof(tk)))
return false;
return true;
}
static int
prestera_util_neigh2nc_key(struct prestera_switch *sw, struct neighbour *n,
struct prestera_kern_neigh_cache_key *key)
{
memset(key, 0, sizeof(*key));
if (n->tbl->family == AF_INET) {
key->addr.v = PRESTERA_IPV4;
key->addr.u.ipv4 = *(__be32 *)n->primary_key;
} else {
return -ENOENT;
}
key->dev = n->dev;
return 0;
}
static bool __prestera_fi_is_direct(struct fib_info *fi)
{
struct fib_nh *fib_nh;
if (fib_info_num_path(fi) == 1) {
fib_nh = fib_info_nh(fi, 0);
if (fib_nh->fib_nh_gw_family == AF_UNSPEC)
return true;
}
return false;
}
static bool prestera_fi_is_direct(struct fib_info *fi)
{
if (fi->fib_type != RTN_UNICAST)
return false;
return __prestera_fi_is_direct(fi);
}
static bool prestera_fi_is_nh(struct fib_info *fi)
{
if (fi->fib_type != RTN_UNICAST)
return false;
return !__prestera_fi_is_direct(fi);
}
static bool __prestera_fi6_is_direct(struct fib6_info *fi)
{
if (!fi->fib6_nh->nh_common.nhc_gw_family)
return true;
return false;
}
static bool prestera_fi6_is_direct(struct fib6_info *fi)
{
if (fi->fib6_type != RTN_UNICAST)
return false;
return __prestera_fi6_is_direct(fi);
}
static bool prestera_fi6_is_nh(struct fib6_info *fi)
{
if (fi->fib6_type != RTN_UNICAST)
return false;
return !__prestera_fi6_is_direct(fi);
}
static bool prestera_fib_info_is_direct(struct fib_notifier_info *info)
{
struct fib6_entry_notifier_info *fen6_info =
container_of(info, struct fib6_entry_notifier_info, info);
struct fib_entry_notifier_info *fen_info =
container_of(info, struct fib_entry_notifier_info, info);
if (info->family == AF_INET)
return prestera_fi_is_direct(fen_info->fi);
else
return prestera_fi6_is_direct(fen6_info->rt);
}
static bool prestera_fib_info_is_nh(struct fib_notifier_info *info)
{
struct fib6_entry_notifier_info *fen6_info =
container_of(info, struct fib6_entry_notifier_info, info);
struct fib_entry_notifier_info *fen_info =
container_of(info, struct fib_entry_notifier_info, info);
if (info->family == AF_INET)
return prestera_fi_is_nh(fen_info->fi);
else
return prestera_fi6_is_nh(fen6_info->rt);
}
/* must be called with rcu_read_lock() */
static int prestera_util_kern_get_route(struct fib_result *res, u32 tb_id,
__be32 *addr)
{
struct flowi4 fl4;
/* TODO: walkthrough appropriate tables in kernel
* to know if the same prefix exists in several tables
*/
memset(&fl4, 0, sizeof(fl4));
fl4.daddr = *addr;
return fib_lookup(&init_net, &fl4, res, 0 /* FIB_LOOKUP_NOREF */);
}
static bool
__prestera_util_kern_n_is_reachable_v4(u32 tb_id, __be32 *addr,
struct net_device *dev)
{
struct fib_nh *fib_nh;
struct fib_result res;
bool reachable;
reachable = false;
if (!prestera_util_kern_get_route(&res, tb_id, addr))
if (prestera_fi_is_direct(res.fi)) {
fib_nh = fib_info_nh(res.fi, 0);
if (dev == fib_nh->fib_nh_dev)
reachable = true;
}
return reachable;
}
/* Check if neigh route is reachable */
static bool
prestera_util_kern_n_is_reachable(u32 tb_id,
struct prestera_ip_addr *addr,
struct net_device *dev)
{
if (addr->v == PRESTERA_IPV4)
return __prestera_util_kern_n_is_reachable_v4(tb_id,
&addr->u.ipv4,
dev);
else
return false;
}
static void prestera_util_kern_set_neigh_offload(struct neighbour *n,
bool offloaded)
{
if (offloaded)
n->flags |= NTF_OFFLOADED;
else
n->flags &= ~NTF_OFFLOADED;
}
static void
prestera_util_kern_set_nh_offload(struct fib_nh_common *nhc, bool offloaded, bool trap)
{
if (offloaded)
nhc->nhc_flags |= RTNH_F_OFFLOAD;
else
nhc->nhc_flags &= ~RTNH_F_OFFLOAD;
if (trap)
nhc->nhc_flags |= RTNH_F_TRAP;
else
nhc->nhc_flags &= ~RTNH_F_TRAP;
}
static struct fib_nh_common *
prestera_kern_fib_info_nhc(struct fib_notifier_info *info, int n)
{
struct fib6_entry_notifier_info *fen6_info;
struct fib_entry_notifier_info *fen4_info;
struct fib6_info *iter;
if (info->family == AF_INET) {
fen4_info = container_of(info, struct fib_entry_notifier_info,
info);
return &fib_info_nh(fen4_info->fi, n)->nh_common;
} else if (info->family == AF_INET6) {
fen6_info = container_of(info, struct fib6_entry_notifier_info,
info);
if (!n)
return &fen6_info->rt->fib6_nh->nh_common;
list_for_each_entry(iter, &fen6_info->rt->fib6_siblings,
fib6_siblings) {
if (!--n)
return &iter->fib6_nh->nh_common;
}
}
/* if family is incorrect - than upper functions has BUG */
/* if doesn't find requested index - there is alsi bug, because
* valid index must be produced by nhs, which checks list length
*/
WARN(1, "Invalid parameters passed to %s n=%d i=%p",
__func__, n, info);
return NULL;
}
static int prestera_kern_fib_info_nhs(struct fib_notifier_info *info)
{
struct fib6_entry_notifier_info *fen6_info;
struct fib_entry_notifier_info *fen4_info;
if (info->family == AF_INET) {
fen4_info = container_of(info, struct fib_entry_notifier_info,
info);
return fib_info_num_path(fen4_info->fi);
} else if (info->family == AF_INET6) {
fen6_info = container_of(info, struct fib6_entry_notifier_info,
info);
return fen6_info->rt->fib6_nsiblings + 1;
}
return 0;
}
static unsigned char
prestera_kern_fib_info_type(struct fib_notifier_info *info)
{
struct fib6_entry_notifier_info *fen6_info;
struct fib_entry_notifier_info *fen4_info;
if (info->family == AF_INET) {
fen4_info = container_of(info, struct fib_entry_notifier_info,
info);
return fen4_info->fi->fib_type;
} else if (info->family == AF_INET6) {
fen6_info = container_of(info, struct fib6_entry_notifier_info,
info);
/* TODO: ECMP in ipv6 is several routes.
* Every route has single nh.
*/
return fen6_info->rt->fib6_type;
}
return RTN_UNSPEC;
}
/* Decided, that uc_nh route with key==nh is obviously neighbour route */
static bool
prestera_fib_node_util_is_neighbour(struct prestera_fib_node *fib_node)
{
if (fib_node->info.type != PRESTERA_FIB_TYPE_UC_NH)
return false;
if (fib_node->info.nh_grp->nh_neigh_head[1].neigh)
return false;
if (!fib_node->info.nh_grp->nh_neigh_head[0].neigh)
return false;
if (memcmp(&fib_node->info.nh_grp->nh_neigh_head[0].neigh->key.addr,
&fib_node->key.addr, sizeof(struct prestera_ip_addr)))
return false;
return true;
}
static int prestera_dev_if_type(const struct net_device *dev)
{
struct macvlan_dev *vlan;
if (is_vlan_dev(dev) &&
netif_is_bridge_master(vlan_dev_real_dev(dev))) {
return PRESTERA_IF_VID_E;
} else if (netif_is_bridge_master(dev)) {
return PRESTERA_IF_VID_E;
} else if (netif_is_lag_master(dev)) {
return PRESTERA_IF_LAG_E;
} else if (netif_is_macvlan(dev)) {
vlan = netdev_priv(dev);
return prestera_dev_if_type(vlan->lowerdev);
} else {
return PRESTERA_IF_PORT_E;
}
}
static int
prestera_neigh_iface_init(struct prestera_switch *sw,
struct prestera_iface *iface,
struct neighbour *n)
{
struct prestera_port *port;
iface->vlan_id = 0; /* TODO: vlan egress */
iface->type = prestera_dev_if_type(n->dev);
if (iface->type != PRESTERA_IF_PORT_E)
return -EINVAL;
if (!prestera_netdev_check(n->dev))
return -EINVAL;
port = netdev_priv(n->dev);
iface->dev_port.hw_dev_num = port->dev_id;
iface->dev_port.port_num = port->hw_id;
return 0;
}
static struct prestera_kern_neigh_cache *
prestera_kern_neigh_cache_find(struct prestera_switch *sw,
struct prestera_kern_neigh_cache_key *key)
{
struct prestera_kern_neigh_cache *n_cache;
n_cache =
rhashtable_lookup_fast(&sw->router->kern_neigh_cache_ht, key,
__prestera_kern_neigh_cache_ht_params);
return IS_ERR(n_cache) ? NULL : n_cache;
}
static void
__prestera_kern_neigh_cache_destruct(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *n_cache)
{
dev_put(n_cache->key.dev);
}
static void
__prestera_kern_neigh_cache_destroy(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *n_cache)
{
rhashtable_remove_fast(&sw->router->kern_neigh_cache_ht,
&n_cache->ht_node,
__prestera_kern_neigh_cache_ht_params);
__prestera_kern_neigh_cache_destruct(sw, n_cache);
kfree(n_cache);
}
static struct prestera_kern_neigh_cache *
__prestera_kern_neigh_cache_create(struct prestera_switch *sw,
struct prestera_kern_neigh_cache_key *key)
{
struct prestera_kern_neigh_cache *n_cache;
int err;
n_cache = kzalloc(sizeof(*n_cache), GFP_KERNEL);
if (!n_cache)
goto err_kzalloc;
memcpy(&n_cache->key, key, sizeof(*key));
dev_hold(n_cache->key.dev);
INIT_LIST_HEAD(&n_cache->kern_fib_cache_list);
err = rhashtable_insert_fast(&sw->router->kern_neigh_cache_ht,
&n_cache->ht_node,
__prestera_kern_neigh_cache_ht_params);
if (err)
goto err_ht_insert;
return n_cache;
err_ht_insert:
dev_put(n_cache->key.dev);
kfree(n_cache);
err_kzalloc:
return NULL;
}
static struct prestera_kern_neigh_cache *
prestera_kern_neigh_cache_get(struct prestera_switch *sw,
struct prestera_kern_neigh_cache_key *key)
{
struct prestera_kern_neigh_cache *n_cache;
n_cache = prestera_kern_neigh_cache_find(sw, key);
if (!n_cache)
n_cache = __prestera_kern_neigh_cache_create(sw, key);
return n_cache;
}
static struct prestera_kern_neigh_cache *
prestera_kern_neigh_cache_put(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *n_cache)
{
if (!n_cache->in_kernel &&
list_empty(&n_cache->kern_fib_cache_list)) {
__prestera_kern_neigh_cache_destroy(sw, n_cache);
return NULL;
}
return n_cache;
}
static struct prestera_kern_fib_cache * static struct prestera_kern_fib_cache *
prestera_kern_fib_cache_find(struct prestera_switch *sw, prestera_kern_fib_cache_find(struct prestera_switch *sw,
struct prestera_kern_fib_cache_key *key) struct prestera_kern_fib_cache_key *key)
...@@ -72,25 +546,80 @@ prestera_kern_fib_cache_find(struct prestera_switch *sw, ...@@ -72,25 +546,80 @@ prestera_kern_fib_cache_find(struct prestera_switch *sw,
return fib_cache; return fib_cache;
} }
static void
__prestera_kern_fib_cache_destruct(struct prestera_switch *sw,
struct prestera_kern_fib_cache *fib_cache)
{
struct prestera_kern_neigh_cache *n_cache;
int i;
for (i = 0; i < PRESTERA_NHGR_SIZE_MAX; i++) {
n_cache = fib_cache->kern_neigh_cache_head[i].n_cache;
if (n_cache) {
list_del(&fib_cache->kern_neigh_cache_head[i].head);
prestera_kern_neigh_cache_put(sw, n_cache);
}
}
fib_info_put(fib_cache->fen4_info.fi);
}
static void static void
prestera_kern_fib_cache_destroy(struct prestera_switch *sw, prestera_kern_fib_cache_destroy(struct prestera_switch *sw,
struct prestera_kern_fib_cache *fib_cache) struct prestera_kern_fib_cache *fib_cache)
{ {
fib_info_put(fib_cache->fi);
rhashtable_remove_fast(&sw->router->kern_fib_cache_ht, rhashtable_remove_fast(&sw->router->kern_fib_cache_ht,
&fib_cache->ht_node, &fib_cache->ht_node,
__prestera_kern_fib_cache_ht_params); __prestera_kern_fib_cache_ht_params);
__prestera_kern_fib_cache_destruct(sw, fib_cache);
kfree(fib_cache); kfree(fib_cache);
} }
static int
__prestera_kern_fib_cache_create_nhs(struct prestera_switch *sw,
struct prestera_kern_fib_cache *fc)
{
struct prestera_kern_neigh_cache_key nc_key;
struct prestera_kern_neigh_cache *n_cache;
struct fib_nh_common *nhc;
int i, nhs, err;
if (!prestera_fib_info_is_nh(&fc->info))
return 0;
nhs = prestera_kern_fib_info_nhs(&fc->info);
if (nhs > PRESTERA_NHGR_SIZE_MAX)
return 0;
for (i = 0; i < nhs; i++) {
nhc = prestera_kern_fib_info_nhc(&fc->fen4_info.info, i);
err = prestera_util_nhc2nc_key(sw, nhc, &nc_key);
if (err)
return 0;
n_cache = prestera_kern_neigh_cache_get(sw, &nc_key);
if (!n_cache)
return 0;
fc->kern_neigh_cache_head[i].this = fc;
fc->kern_neigh_cache_head[i].n_cache = n_cache;
list_add(&fc->kern_neigh_cache_head[i].head,
&n_cache->kern_fib_cache_list);
}
return 0;
}
/* Operations on fi (offload, etc) must be wrapped in utils. /* Operations on fi (offload, etc) must be wrapped in utils.
* This function just create storage. * This function just create storage.
*/ */
static struct prestera_kern_fib_cache * static struct prestera_kern_fib_cache *
prestera_kern_fib_cache_create(struct prestera_switch *sw, prestera_kern_fib_cache_create(struct prestera_switch *sw,
struct prestera_kern_fib_cache_key *key, struct prestera_kern_fib_cache_key *key,
struct fib_info *fi, dscp_t dscp, u8 type) struct fib_notifier_info *info)
{ {
struct fib_entry_notifier_info *fen_info =
container_of(info, struct fib_entry_notifier_info, info);
struct prestera_kern_fib_cache *fib_cache; struct prestera_kern_fib_cache *fib_cache;
int err; int err;
...@@ -99,10 +628,8 @@ prestera_kern_fib_cache_create(struct prestera_switch *sw, ...@@ -99,10 +628,8 @@ prestera_kern_fib_cache_create(struct prestera_switch *sw,
goto err_kzalloc; goto err_kzalloc;
memcpy(&fib_cache->key, key, sizeof(*key)); memcpy(&fib_cache->key, key, sizeof(*key));
fib_info_hold(fi); fib_info_hold(fen_info->fi);
fib_cache->fi = fi; memcpy(&fib_cache->fen4_info, fen_info, sizeof(*fen_info));
fib_cache->kern_dscp = dscp;
fib_cache->kern_type = type;
err = rhashtable_insert_fast(&sw->router->kern_fib_cache_ht, err = rhashtable_insert_fast(&sw->router->kern_fib_cache_ht,
&fib_cache->ht_node, &fib_cache->ht_node,
...@@ -110,15 +637,61 @@ prestera_kern_fib_cache_create(struct prestera_switch *sw, ...@@ -110,15 +637,61 @@ prestera_kern_fib_cache_create(struct prestera_switch *sw,
if (err) if (err)
goto err_ht_insert; goto err_ht_insert;
/* Handle nexthops */
err = __prestera_kern_fib_cache_create_nhs(sw, fib_cache);
if (err)
goto out; /* Not critical */
out:
return fib_cache; return fib_cache;
err_ht_insert: err_ht_insert:
fib_info_put(fi); fib_info_put(fen_info->fi);
kfree(fib_cache); kfree(fib_cache);
err_kzalloc: err_kzalloc:
return NULL; return NULL;
} }
static void
__prestera_k_arb_fib_nh_offload_set(struct prestera_switch *sw,
struct prestera_kern_fib_cache *fibc,
struct prestera_kern_neigh_cache *nc,
bool offloaded, bool trap)
{
struct fib_nh_common *nhc;
int i, nhs;
nhs = prestera_kern_fib_info_nhs(&fibc->info);
for (i = 0; i < nhs; i++) {
nhc = prestera_kern_fib_info_nhc(&fibc->info, i);
if (!nc) {
prestera_util_kern_set_nh_offload(nhc, offloaded, trap);
continue;
}
if (prestera_util_nhc_eq_n_cache_key(sw, nhc, &nc->key)) {
prestera_util_kern_set_nh_offload(nhc, offloaded, trap);
break;
}
}
}
static void
__prestera_k_arb_n_offload_set(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *nc,
bool offloaded)
{
struct neighbour *n;
n = neigh_lookup(&arp_tbl, &nc->key.addr.u.ipv4,
nc->key.dev);
if (!n)
return;
prestera_util_kern_set_neigh_offload(n, offloaded);
neigh_release(n);
}
static void static void
__prestera_k_arb_fib_lpm_offload_set(struct prestera_switch *sw, __prestera_k_arb_fib_lpm_offload_set(struct prestera_switch *sw,
struct prestera_kern_fib_cache *fc, struct prestera_kern_fib_cache *fc,
...@@ -126,32 +699,208 @@ __prestera_k_arb_fib_lpm_offload_set(struct prestera_switch *sw, ...@@ -126,32 +699,208 @@ __prestera_k_arb_fib_lpm_offload_set(struct prestera_switch *sw,
{ {
struct fib_rt_info fri; struct fib_rt_info fri;
if (fc->key.addr.v != PRESTERA_IPV4) switch (fc->key.addr.v) {
case PRESTERA_IPV4:
fri.fi = fc->fen4_info.fi;
fri.tb_id = fc->key.kern_tb_id;
fri.dst = fc->key.addr.u.ipv4;
fri.dst_len = fc->key.prefix_len;
fri.dscp = fc->fen4_info.dscp;
fri.type = fc->fen4_info.type;
/* flags begin */
fri.offload = offload;
fri.trap = trap;
fri.offload_failed = fail;
/* flags end */
fib_alias_hw_flags_set(&init_net, &fri);
return;
case PRESTERA_IPV6:
/* TODO */
return; return;
}
}
static void
__prestera_k_arb_n_lpm_set(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *n_cache,
bool enabled)
{
struct prestera_nexthop_group_key nh_grp_key;
struct prestera_kern_fib_cache_key fc_key;
struct prestera_kern_fib_cache *fib_cache;
struct prestera_fib_node *fib_node;
struct prestera_fib_key fib_key;
/* Exception for fc with prefix 32: LPM entry is already used by fib */
memset(&fc_key, 0, sizeof(fc_key));
fc_key.addr = n_cache->key.addr;
fc_key.prefix_len = PRESTERA_IP_ADDR_PLEN(n_cache->key.addr.v);
/* But better to use tb_id of route, which pointed to this neighbour. */
/* We take it from rif, because rif inconsistent.
* Must be separated in_rif and out_rif.
* Also note: for each fib pointed to this neigh should be separated
* neigh lpm entry (for each ingress vr)
*/
fc_key.kern_tb_id = l3mdev_fib_table(n_cache->key.dev);
fib_cache = prestera_kern_fib_cache_find(sw, &fc_key);
memset(&fib_key, 0, sizeof(fib_key));
fib_key.addr = n_cache->key.addr;
fib_key.prefix_len = PRESTERA_IP_ADDR_PLEN(n_cache->key.addr.v);
fib_key.tb_id = prestera_fix_tb_id(fc_key.kern_tb_id);
fib_node = prestera_fib_node_find(sw, &fib_key);
if (!fib_cache || !fib_cache->reachable) {
if (!enabled && fib_node) {
if (prestera_fib_node_util_is_neighbour(fib_node))
prestera_fib_node_destroy(sw, fib_node);
return;
}
}
if (enabled && !fib_node) {
memset(&nh_grp_key, 0, sizeof(nh_grp_key));
prestera_util_nc_key2nh_key(&n_cache->key,
&nh_grp_key.neigh[0]);
fib_node = prestera_fib_node_create(sw, &fib_key,
PRESTERA_FIB_TYPE_UC_NH,
&nh_grp_key);
if (!fib_node)
pr_err("%s failed ip=%pI4n", "prestera_fib_node_create",
&fib_key.addr.u.ipv4);
return;
}
}
static void
__prestera_k_arb_nc_kern_fib_fetch(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *nc)
{
if (prestera_util_kern_n_is_reachable(l3mdev_fib_table(nc->key.dev),
&nc->key.addr, nc->key.dev))
nc->reachable = true;
else
nc->reachable = false;
}
/* Kernel neighbour -> neigh_cache info */
static void
__prestera_k_arb_nc_kern_n_fetch(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *nc)
{
struct neighbour *n;
int err;
memset(&nc->nh_neigh_info, 0, sizeof(nc->nh_neigh_info));
n = neigh_lookup(&arp_tbl, &nc->key.addr.u.ipv4, nc->key.dev);
if (!n)
goto out;
read_lock_bh(&n->lock);
if (n->nud_state & NUD_VALID && !n->dead) {
err = prestera_neigh_iface_init(sw, &nc->nh_neigh_info.iface,
n);
if (err)
goto n_read_out;
fri.fi = fc->fi; memcpy(&nc->nh_neigh_info.ha[0], &n->ha[0], ETH_ALEN);
fri.tb_id = fc->key.kern_tb_id; nc->nh_neigh_info.connected = true;
fri.dst = fc->key.addr.u.ipv4; }
fri.dst_len = fc->key.prefix_len; n_read_out:
fri.dscp = fc->kern_dscp; read_unlock_bh(&n->lock);
fri.type = fc->kern_type; out:
/* flags begin */ nc->in_kernel = nc->nh_neigh_info.connected;
fri.offload = offload; if (n)
fri.trap = trap; neigh_release(n);
fri.offload_failed = fail; }
/* flags end */
fib_alias_hw_flags_set(&init_net, &fri); /* neigh_cache info -> lpm update */
static void
__prestera_k_arb_nc_apply(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *nc)
{
struct prestera_kern_neigh_cache_head *nhead;
struct prestera_nh_neigh_key nh_key;
struct prestera_nh_neigh *nh_neigh;
int err;
__prestera_k_arb_n_lpm_set(sw, nc, nc->reachable && nc->in_kernel);
__prestera_k_arb_n_offload_set(sw, nc, nc->reachable && nc->in_kernel);
prestera_util_nc_key2nh_key(&nc->key, &nh_key);
nh_neigh = prestera_nh_neigh_find(sw, &nh_key);
if (!nh_neigh)
goto out;
/* Do hw update only if something changed to prevent nh flap */
if (memcmp(&nc->nh_neigh_info, &nh_neigh->info,
sizeof(nh_neigh->info))) {
memcpy(&nh_neigh->info, &nc->nh_neigh_info,
sizeof(nh_neigh->info));
err = prestera_nh_neigh_set(sw, nh_neigh);
if (err) {
pr_err("%s failed with err=%d ip=%pI4n mac=%pM",
"prestera_nh_neigh_set", err,
&nh_neigh->key.addr.u.ipv4,
&nh_neigh->info.ha[0]);
goto out;
}
}
out:
list_for_each_entry(nhead, &nc->kern_fib_cache_list, head) {
__prestera_k_arb_fib_nh_offload_set(sw, nhead->this, nc,
nc->in_kernel,
!nc->in_kernel);
}
} }
static int static int
__prestera_pr_k_arb_fc_lpm_info_calc(struct prestera_switch *sw, __prestera_pr_k_arb_fc_lpm_info_calc(struct prestera_switch *sw,
struct prestera_kern_fib_cache *fc) struct prestera_kern_fib_cache *fc)
{ {
struct fib_nh_common *nhc;
int nh_cnt;
memset(&fc->lpm_info, 0, sizeof(fc->lpm_info)); memset(&fc->lpm_info, 0, sizeof(fc->lpm_info));
switch (fc->fi->fib_type) { switch (prestera_kern_fib_info_type(&fc->info)) {
case RTN_UNICAST: case RTN_UNICAST:
fc->lpm_info.fib_type = PRESTERA_FIB_TYPE_TRAP; if (prestera_fib_info_is_direct(&fc->info) &&
fc->key.prefix_len ==
PRESTERA_IP_ADDR_PLEN(fc->key.addr.v)) {
/* This is special case.
* When prefix is 32. Than we will have conflict in lpm
* for direct route - once TRAP added, there is no
* place for neighbour entry. So represent direct route
* with prefix 32, as NH. So neighbour will be resolved
* as nexthop of this route.
*/
nhc = prestera_kern_fib_info_nhc(&fc->info, 0);
fc->lpm_info.fib_type = PRESTERA_FIB_TYPE_UC_NH;
fc->lpm_info.nh_grp_key.neigh[0].addr =
fc->key.addr;
fc->lpm_info.nh_grp_key.neigh[0].rif =
nhc->nhc_dev;
break;
}
/* We can also get nh_grp_key from fi. This will be correct to
* because cache not always represent, what actually written to
* lpm. But we use nh cache, as well for now (for this case).
*/
for (nh_cnt = 0; nh_cnt < PRESTERA_NHGR_SIZE_MAX; nh_cnt++) {
if (!fc->kern_neigh_cache_head[nh_cnt].n_cache)
break;
fc->lpm_info.nh_grp_key.neigh[nh_cnt].addr =
fc->kern_neigh_cache_head[nh_cnt].n_cache->key.addr;
fc->lpm_info.nh_grp_key.neigh[nh_cnt].rif =
fc->kern_neigh_cache_head[nh_cnt].n_cache->key.dev;
}
fc->lpm_info.fib_type = nh_cnt ?
PRESTERA_FIB_TYPE_UC_NH :
PRESTERA_FIB_TYPE_TRAP;
break; break;
/* Unsupported. Leave it for kernel: */ /* Unsupported. Leave it for kernel: */
case RTN_BROADCAST: case RTN_BROADCAST:
...@@ -191,7 +940,8 @@ static int __prestera_k_arb_f_lpm_set(struct prestera_switch *sw, ...@@ -191,7 +940,8 @@ static int __prestera_k_arb_f_lpm_set(struct prestera_switch *sw,
return 0; return 0;
fib_node = prestera_fib_node_create(sw, &fc->lpm_info.fib_key, fib_node = prestera_fib_node_create(sw, &fc->lpm_info.fib_key,
fc->lpm_info.fib_type); fc->lpm_info.fib_type,
&fc->lpm_info.nh_grp_key);
if (!fib_node) { if (!fib_node) {
dev_err(sw->dev->dev, "fib_node=NULL %pI4n/%d kern_tb_id = %d", dev_err(sw->dev->dev, "fib_node=NULL %pI4n/%d kern_tb_id = %d",
...@@ -220,6 +970,10 @@ static int __prestera_k_arb_fc_apply(struct prestera_switch *sw, ...@@ -220,6 +970,10 @@ static int __prestera_k_arb_fc_apply(struct prestera_switch *sw,
} }
switch (fc->lpm_info.fib_type) { switch (fc->lpm_info.fib_type) {
case PRESTERA_FIB_TYPE_UC_NH:
__prestera_k_arb_fib_lpm_offload_set(sw, fc, false,
fc->reachable, false);
break;
case PRESTERA_FIB_TYPE_TRAP: case PRESTERA_FIB_TYPE_TRAP:
__prestera_k_arb_fib_lpm_offload_set(sw, fc, false, __prestera_k_arb_fib_lpm_offload_set(sw, fc, false,
false, fc->reachable); false, fc->reachable);
...@@ -271,17 +1025,140 @@ __prestera_k_arb_util_fib_overlapped(struct prestera_switch *sw, ...@@ -271,17 +1025,140 @@ __prestera_k_arb_util_fib_overlapped(struct prestera_switch *sw,
return rfc; return rfc;
} }
static void __prestera_k_arb_hw_state_upd(struct prestera_switch *sw,
struct prestera_kern_neigh_cache *nc)
{
struct prestera_nh_neigh_key nh_key;
struct prestera_nh_neigh *nh_neigh;
struct neighbour *n;
bool hw_active;
prestera_util_nc_key2nh_key(&nc->key, &nh_key);
nh_neigh = prestera_nh_neigh_find(sw, &nh_key);
if (!nh_neigh) {
pr_err("Cannot find nh_neigh for cached %pI4n",
&nc->key.addr.u.ipv4);
return;
}
hw_active = prestera_nh_neigh_util_hw_state(sw, nh_neigh);
#ifdef PRESTERA_IMPLICITY_RESOLVE_DEAD_NEIGH
if (!hw_active && nc->in_kernel)
goto out;
#else /* PRESTERA_IMPLICITY_RESOLVE_DEAD_NEIGH */
if (!hw_active)
goto out;
#endif /* PRESTERA_IMPLICITY_RESOLVE_DEAD_NEIGH */
if (nc->key.addr.v == PRESTERA_IPV4) {
n = neigh_lookup(&arp_tbl, &nc->key.addr.u.ipv4,
nc->key.dev);
if (!n)
n = neigh_create(&arp_tbl, &nc->key.addr.u.ipv4,
nc->key.dev);
} else {
n = NULL;
}
if (!IS_ERR(n) && n) {
neigh_event_send(n, NULL);
neigh_release(n);
} else {
pr_err("Cannot create neighbour %pI4n", &nc->key.addr.u.ipv4);
}
out:
return;
}
/* Propagate hw state to kernel */
static void prestera_k_arb_hw_evt(struct prestera_switch *sw)
{
struct prestera_kern_neigh_cache *n_cache;
struct rhashtable_iter iter;
rhashtable_walk_enter(&sw->router->kern_neigh_cache_ht, &iter);
rhashtable_walk_start(&iter);
while (1) {
n_cache = rhashtable_walk_next(&iter);
if (!n_cache)
break;
if (IS_ERR(n_cache))
continue;
rhashtable_walk_stop(&iter);
__prestera_k_arb_hw_state_upd(sw, n_cache);
rhashtable_walk_start(&iter);
}
rhashtable_walk_stop(&iter);
rhashtable_walk_exit(&iter);
}
/* Propagate kernel event to hw */
static void prestera_k_arb_n_evt(struct prestera_switch *sw,
struct neighbour *n)
{
struct prestera_kern_neigh_cache_key n_key;
struct prestera_kern_neigh_cache *n_cache;
int err;
err = prestera_util_neigh2nc_key(sw, n, &n_key);
if (err)
return;
n_cache = prestera_kern_neigh_cache_find(sw, &n_key);
if (!n_cache) {
n_cache = prestera_kern_neigh_cache_get(sw, &n_key);
if (!n_cache)
return;
__prestera_k_arb_nc_kern_fib_fetch(sw, n_cache);
}
__prestera_k_arb_nc_kern_n_fetch(sw, n_cache);
__prestera_k_arb_nc_apply(sw, n_cache);
prestera_kern_neigh_cache_put(sw, n_cache);
}
static void __prestera_k_arb_fib_evt2nc(struct prestera_switch *sw)
{
struct prestera_kern_neigh_cache *n_cache;
struct rhashtable_iter iter;
rhashtable_walk_enter(&sw->router->kern_neigh_cache_ht, &iter);
rhashtable_walk_start(&iter);
while (1) {
n_cache = rhashtable_walk_next(&iter);
if (!n_cache)
break;
if (IS_ERR(n_cache))
continue;
rhashtable_walk_stop(&iter);
__prestera_k_arb_nc_kern_fib_fetch(sw, n_cache);
__prestera_k_arb_nc_apply(sw, n_cache);
rhashtable_walk_start(&iter);
}
rhashtable_walk_stop(&iter);
rhashtable_walk_exit(&iter);
}
static int static int
prestera_k_arb_fib_evt(struct prestera_switch *sw, prestera_k_arb_fib_evt(struct prestera_switch *sw,
bool replace, /* replace or del */ bool replace, /* replace or del */
struct fib_entry_notifier_info *fen_info) struct fib_notifier_info *info)
{ {
struct prestera_kern_fib_cache *tfib_cache, *bfib_cache; /* top/btm */ struct prestera_kern_fib_cache *tfib_cache, *bfib_cache; /* top/btm */
struct prestera_kern_fib_cache_key fc_key; struct prestera_kern_fib_cache_key fc_key;
struct prestera_kern_fib_cache *fib_cache; struct prestera_kern_fib_cache *fib_cache;
int err; int err;
prestera_util_fen_info2fib_cache_key(fen_info, &fc_key); prestera_util_fen_info2fib_cache_key(info, &fc_key);
fib_cache = prestera_kern_fib_cache_find(sw, &fc_key); fib_cache = prestera_kern_fib_cache_find(sw, &fc_key);
if (fib_cache) { if (fib_cache) {
fib_cache->reachable = false; fib_cache->reachable = false;
...@@ -304,10 +1181,7 @@ prestera_k_arb_fib_evt(struct prestera_switch *sw, ...@@ -304,10 +1181,7 @@ prestera_k_arb_fib_evt(struct prestera_switch *sw,
} }
if (replace) { if (replace) {
fib_cache = prestera_kern_fib_cache_create(sw, &fc_key, fib_cache = prestera_kern_fib_cache_create(sw, &fc_key, info);
fen_info->fi,
fen_info->dscp,
fen_info->type);
if (!fib_cache) { if (!fib_cache) {
dev_err(sw->dev->dev, "fib_cache == NULL"); dev_err(sw->dev->dev, "fib_cache == NULL");
return -ENOENT; return -ENOENT;
...@@ -331,9 +1205,65 @@ prestera_k_arb_fib_evt(struct prestera_switch *sw, ...@@ -331,9 +1205,65 @@ prestera_k_arb_fib_evt(struct prestera_switch *sw,
dev_err(sw->dev->dev, "Applying fib_cache failed"); dev_err(sw->dev->dev, "Applying fib_cache failed");
} }
/* Update all neighs to resolve overlapped and apply related */
__prestera_k_arb_fib_evt2nc(sw);
return 0; return 0;
} }
static void __prestera_k_arb_abort_neigh_ht_cb(void *ptr, void *arg)
{
struct prestera_kern_neigh_cache *n_cache = ptr;
struct prestera_switch *sw = arg;
if (!list_empty(&n_cache->kern_fib_cache_list)) {
WARN_ON(1); /* BUG */
return;
}
__prestera_k_arb_n_offload_set(sw, n_cache, false);
n_cache->in_kernel = false;
/* No need to destroy lpm.
* It will be aborted by destroy_ht
*/
__prestera_kern_neigh_cache_destruct(sw, n_cache);
kfree(n_cache);
}
static void __prestera_k_arb_abort_fib_ht_cb(void *ptr, void *arg)
{
struct prestera_kern_fib_cache *fib_cache = ptr;
struct prestera_switch *sw = arg;
__prestera_k_arb_fib_lpm_offload_set(sw, fib_cache,
false, false,
false);
__prestera_k_arb_fib_nh_offload_set(sw, fib_cache, NULL,
false, false);
/* No need to destroy lpm.
* It will be aborted by destroy_ht
*/
__prestera_kern_fib_cache_destruct(sw, fib_cache);
kfree(fib_cache);
}
static void prestera_k_arb_abort(struct prestera_switch *sw)
{
/* Function to remove all arbiter entries and related hw objects. */
/* Sequence:
* 1) Clear arbiter tables, but don't touch hw
* 2) Clear hw
* We use such approach, because arbiter object is not directly mapped
* to hw. So deletion of one arbiter object may even lead to creation of
* hw object (e.g. in case of overlapped routes).
*/
rhashtable_free_and_destroy(&sw->router->kern_fib_cache_ht,
__prestera_k_arb_abort_fib_ht_cb,
sw);
rhashtable_free_and_destroy(&sw->router->kern_neigh_cache_ht,
__prestera_k_arb_abort_neigh_ht_cb,
sw);
}
static int __prestera_inetaddr_port_event(struct net_device *port_dev, static int __prestera_inetaddr_port_event(struct net_device *port_dev,
unsigned long event, unsigned long event,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
...@@ -469,13 +1399,15 @@ static void __prestera_router_fib_event_work(struct work_struct *work) ...@@ -469,13 +1399,15 @@ static void __prestera_router_fib_event_work(struct work_struct *work)
switch (fib_work->event) { switch (fib_work->event) {
case FIB_EVENT_ENTRY_REPLACE: case FIB_EVENT_ENTRY_REPLACE:
err = prestera_k_arb_fib_evt(sw, true, &fib_work->fen_info); err = prestera_k_arb_fib_evt(sw, true,
&fib_work->fen_info.info);
if (err) if (err)
goto err_out; goto err_out;
break; break;
case FIB_EVENT_ENTRY_DEL: case FIB_EVENT_ENTRY_DEL:
err = prestera_k_arb_fib_evt(sw, false, &fib_work->fen_info); err = prestera_k_arb_fib_evt(sw, false,
&fib_work->fen_info.info);
if (err) if (err)
goto err_out; goto err_out;
...@@ -534,10 +1466,89 @@ static int __prestera_router_fib_event(struct notifier_block *nb, ...@@ -534,10 +1466,89 @@ static int __prestera_router_fib_event(struct notifier_block *nb,
return NOTIFY_DONE; return NOTIFY_DONE;
} }
struct prestera_netevent_work {
struct work_struct work;
struct prestera_switch *sw;
struct neighbour *n;
};
static void prestera_router_neigh_event_work(struct work_struct *work)
{
struct prestera_netevent_work *net_work =
container_of(work, struct prestera_netevent_work, work);
struct prestera_switch *sw = net_work->sw;
struct neighbour *n = net_work->n;
/* neigh - its not hw related object. It stored only in kernel. So... */
rtnl_lock();
prestera_k_arb_n_evt(sw, n);
neigh_release(n);
rtnl_unlock();
kfree(net_work);
}
static int prestera_router_netevent_event(struct notifier_block *nb,
unsigned long event, void *ptr)
{
struct prestera_netevent_work *net_work;
struct prestera_router *router;
struct neighbour *n = ptr;
router = container_of(nb, struct prestera_router, netevent_nb);
switch (event) {
case NETEVENT_NEIGH_UPDATE:
if (n->tbl->family != AF_INET)
return NOTIFY_DONE;
net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
if (WARN_ON(!net_work))
return NOTIFY_BAD;
neigh_clone(n);
net_work->n = n;
net_work->sw = router->sw;
INIT_WORK(&net_work->work, prestera_router_neigh_event_work);
prestera_queue_work(&net_work->work);
}
return NOTIFY_DONE;
}
static void prestera_router_update_neighs_work(struct work_struct *work)
{
struct prestera_router *router;
router = container_of(work, struct prestera_router,
neighs_update.dw.work);
rtnl_lock();
prestera_k_arb_hw_evt(router->sw);
rtnl_unlock();
prestera_queue_delayed_work(&router->neighs_update.dw,
msecs_to_jiffies(PRESTERA_NH_PROBE_INTERVAL));
}
static int prestera_neigh_work_init(struct prestera_switch *sw)
{
INIT_DELAYED_WORK(&sw->router->neighs_update.dw,
prestera_router_update_neighs_work);
prestera_queue_delayed_work(&sw->router->neighs_update.dw, 0);
return 0;
}
static void prestera_neigh_work_fini(struct prestera_switch *sw)
{
cancel_delayed_work_sync(&sw->router->neighs_update.dw);
}
int prestera_router_init(struct prestera_switch *sw) int prestera_router_init(struct prestera_switch *sw)
{ {
struct prestera_router *router; struct prestera_router *router;
int err; int err, nhgrp_cache_bytes;
router = kzalloc(sizeof(*sw->router), GFP_KERNEL); router = kzalloc(sizeof(*sw->router), GFP_KERNEL);
if (!router) if (!router)
...@@ -555,6 +1566,22 @@ int prestera_router_init(struct prestera_switch *sw) ...@@ -555,6 +1566,22 @@ int prestera_router_init(struct prestera_switch *sw)
if (err) if (err)
goto err_kern_fib_cache_ht_init; goto err_kern_fib_cache_ht_init;
err = rhashtable_init(&router->kern_neigh_cache_ht,
&__prestera_kern_neigh_cache_ht_params);
if (err)
goto err_kern_neigh_cache_ht_init;
nhgrp_cache_bytes = sw->size_tbl_router_nexthop / 8 + 1;
router->nhgrp_hw_state_cache = kzalloc(nhgrp_cache_bytes, GFP_KERNEL);
if (!router->nhgrp_hw_state_cache) {
err = -ENOMEM;
goto err_nh_state_cache_alloc;
}
err = prestera_neigh_work_init(sw);
if (err)
goto err_neigh_work_init;
router->inetaddr_valid_nb.notifier_call = __prestera_inetaddr_valid_cb; router->inetaddr_valid_nb.notifier_call = __prestera_inetaddr_valid_cb;
err = register_inetaddr_validator_notifier(&router->inetaddr_valid_nb); err = register_inetaddr_validator_notifier(&router->inetaddr_valid_nb);
if (err) if (err)
...@@ -565,6 +1592,11 @@ int prestera_router_init(struct prestera_switch *sw) ...@@ -565,6 +1592,11 @@ int prestera_router_init(struct prestera_switch *sw)
if (err) if (err)
goto err_register_inetaddr_notifier; goto err_register_inetaddr_notifier;
router->netevent_nb.notifier_call = prestera_router_netevent_event;
err = register_netevent_notifier(&router->netevent_nb);
if (err)
goto err_register_netevent_notifier;
router->fib_nb.notifier_call = __prestera_router_fib_event; router->fib_nb.notifier_call = __prestera_router_fib_event;
err = register_fib_notifier(&init_net, &router->fib_nb, err = register_fib_notifier(&init_net, &router->fib_nb,
/* TODO: flush fib entries */ NULL, NULL); /* TODO: flush fib entries */ NULL, NULL);
...@@ -574,10 +1606,18 @@ int prestera_router_init(struct prestera_switch *sw) ...@@ -574,10 +1606,18 @@ int prestera_router_init(struct prestera_switch *sw)
return 0; return 0;
err_register_fib_notifier: err_register_fib_notifier:
unregister_netevent_notifier(&router->netevent_nb);
err_register_netevent_notifier:
unregister_inetaddr_notifier(&router->inetaddr_nb); unregister_inetaddr_notifier(&router->inetaddr_nb);
err_register_inetaddr_notifier: err_register_inetaddr_notifier:
unregister_inetaddr_validator_notifier(&router->inetaddr_valid_nb); unregister_inetaddr_validator_notifier(&router->inetaddr_valid_nb);
err_register_inetaddr_validator_notifier: err_register_inetaddr_validator_notifier:
prestera_neigh_work_fini(sw);
err_neigh_work_init:
kfree(router->nhgrp_hw_state_cache);
err_nh_state_cache_alloc:
rhashtable_destroy(&router->kern_neigh_cache_ht);
err_kern_neigh_cache_ht_init:
rhashtable_destroy(&router->kern_fib_cache_ht); rhashtable_destroy(&router->kern_fib_cache_ht);
err_kern_fib_cache_ht_init: err_kern_fib_cache_ht_init:
prestera_router_hw_fini(sw); prestera_router_hw_fini(sw);
...@@ -589,8 +1629,15 @@ int prestera_router_init(struct prestera_switch *sw) ...@@ -589,8 +1629,15 @@ int prestera_router_init(struct prestera_switch *sw)
void prestera_router_fini(struct prestera_switch *sw) void prestera_router_fini(struct prestera_switch *sw)
{ {
unregister_fib_notifier(&init_net, &sw->router->fib_nb); unregister_fib_notifier(&init_net, &sw->router->fib_nb);
unregister_netevent_notifier(&sw->router->netevent_nb);
unregister_inetaddr_notifier(&sw->router->inetaddr_nb); unregister_inetaddr_notifier(&sw->router->inetaddr_nb);
unregister_inetaddr_validator_notifier(&sw->router->inetaddr_valid_nb); unregister_inetaddr_validator_notifier(&sw->router->inetaddr_valid_nb);
prestera_neigh_work_fini(sw);
prestera_queue_drain();
prestera_k_arb_abort(sw);
kfree(sw->router->nhgrp_hw_state_cache);
rhashtable_destroy(&sw->router->kern_fib_cache_ht); rhashtable_destroy(&sw->router->kern_fib_cache_ht);
prestera_router_hw_fini(sw); prestera_router_hw_fini(sw);
kfree(sw->router); kfree(sw->router);
......
...@@ -8,10 +8,16 @@ ...@@ -8,10 +8,16 @@
#include "prestera_router_hw.h" #include "prestera_router_hw.h"
#include "prestera_acl.h" #include "prestera_acl.h"
/* +--+ /* Nexthop is pointed
* +------->|vr|<-+ * to port (not rif)
* | +--+ | * +-------+
* | | * +>|nexthop|
* | +-------+
* |
* +--+ +-----++
* +------->|vr|<-+ +>|nh_grp|
* | +--+ | | +------+
* | | |
* +-+-------+ +--+---+-+ * +-+-------+ +--+---+-+
* |rif_entry| |fib_node| * |rif_entry| |fib_node|
* +---------+ +--------+ * +---------+ +--------+
...@@ -23,6 +29,8 @@ ...@@ -23,6 +29,8 @@
#define PRESTERA_NHGR_UNUSED (0) #define PRESTERA_NHGR_UNUSED (0)
#define PRESTERA_NHGR_DROP (0xFFFFFFFF) #define PRESTERA_NHGR_DROP (0xFFFFFFFF)
/* Need to merge it with router_manager */
#define PRESTERA_NH_ACTIVE_JIFFER_FILTER 3000 /* ms */
static const struct rhashtable_params __prestera_fib_ht_params = { static const struct rhashtable_params __prestera_fib_ht_params = {
.key_offset = offsetof(struct prestera_fib_node, key), .key_offset = offsetof(struct prestera_fib_node, key),
...@@ -31,10 +39,45 @@ static const struct rhashtable_params __prestera_fib_ht_params = { ...@@ -31,10 +39,45 @@ static const struct rhashtable_params __prestera_fib_ht_params = {
.automatic_shrinking = true, .automatic_shrinking = true,
}; };
static const struct rhashtable_params __prestera_nh_neigh_ht_params = {
.key_offset = offsetof(struct prestera_nh_neigh, key),
.key_len = sizeof(struct prestera_nh_neigh_key),
.head_offset = offsetof(struct prestera_nh_neigh, ht_node),
};
static const struct rhashtable_params __prestera_nexthop_group_ht_params = {
.key_offset = offsetof(struct prestera_nexthop_group, key),
.key_len = sizeof(struct prestera_nexthop_group_key),
.head_offset = offsetof(struct prestera_nexthop_group, ht_node),
};
static int prestera_nexthop_group_set(struct prestera_switch *sw,
struct prestera_nexthop_group *nh_grp);
static bool
prestera_nexthop_group_util_hw_state(struct prestera_switch *sw,
struct prestera_nexthop_group *nh_grp);
static void prestera_fib_node_destroy_ht_cb(void *ptr, void *arg);
/* TODO: move to router.h as macros */
static bool prestera_nh_neigh_key_is_valid(struct prestera_nh_neigh_key *key)
{
return memchr_inv(key, 0, sizeof(*key)) ? true : false;
}
int prestera_router_hw_init(struct prestera_switch *sw) int prestera_router_hw_init(struct prestera_switch *sw)
{ {
int err; int err;
err = rhashtable_init(&sw->router->nh_neigh_ht,
&__prestera_nh_neigh_ht_params);
if (err)
goto err_nh_neigh_ht_init;
err = rhashtable_init(&sw->router->nexthop_group_ht,
&__prestera_nexthop_group_ht_params);
if (err)
goto err_nexthop_grp_ht_init;
err = rhashtable_init(&sw->router->fib_ht, err = rhashtable_init(&sw->router->fib_ht,
&__prestera_fib_ht_params); &__prestera_fib_ht_params);
if (err) if (err)
...@@ -43,15 +86,25 @@ int prestera_router_hw_init(struct prestera_switch *sw) ...@@ -43,15 +86,25 @@ int prestera_router_hw_init(struct prestera_switch *sw)
INIT_LIST_HEAD(&sw->router->vr_list); INIT_LIST_HEAD(&sw->router->vr_list);
INIT_LIST_HEAD(&sw->router->rif_entry_list); INIT_LIST_HEAD(&sw->router->rif_entry_list);
return 0;
err_fib_ht_init: err_fib_ht_init:
rhashtable_destroy(&sw->router->nexthop_group_ht);
err_nexthop_grp_ht_init:
rhashtable_destroy(&sw->router->nh_neigh_ht);
err_nh_neigh_ht_init:
return 0; return 0;
} }
void prestera_router_hw_fini(struct prestera_switch *sw) void prestera_router_hw_fini(struct prestera_switch *sw)
{ {
rhashtable_free_and_destroy(&sw->router->fib_ht,
prestera_fib_node_destroy_ht_cb, sw);
WARN_ON(!list_empty(&sw->router->vr_list)); WARN_ON(!list_empty(&sw->router->vr_list));
WARN_ON(!list_empty(&sw->router->rif_entry_list)); WARN_ON(!list_empty(&sw->router->rif_entry_list));
rhashtable_destroy(&sw->router->fib_ht); rhashtable_destroy(&sw->router->fib_ht);
rhashtable_destroy(&sw->router->nexthop_group_ht);
rhashtable_destroy(&sw->router->nh_neigh_ht);
} }
static struct prestera_vr *__prestera_vr_find(struct prestera_switch *sw, static struct prestera_vr *__prestera_vr_find(struct prestera_switch *sw,
...@@ -232,6 +285,286 @@ prestera_rif_entry_create(struct prestera_switch *sw, ...@@ -232,6 +285,286 @@ prestera_rif_entry_create(struct prestera_switch *sw,
return NULL; return NULL;
} }
static void __prestera_nh_neigh_destroy(struct prestera_switch *sw,
struct prestera_nh_neigh *neigh)
{
rhashtable_remove_fast(&sw->router->nh_neigh_ht,
&neigh->ht_node,
__prestera_nh_neigh_ht_params);
kfree(neigh);
}
static struct prestera_nh_neigh *
__prestera_nh_neigh_create(struct prestera_switch *sw,
struct prestera_nh_neigh_key *key)
{
struct prestera_nh_neigh *neigh;
int err;
neigh = kzalloc(sizeof(*neigh), GFP_KERNEL);
if (!neigh)
goto err_kzalloc;
memcpy(&neigh->key, key, sizeof(*key));
neigh->info.connected = false;
INIT_LIST_HEAD(&neigh->nexthop_group_list);
err = rhashtable_insert_fast(&sw->router->nh_neigh_ht,
&neigh->ht_node,
__prestera_nh_neigh_ht_params);
if (err)
goto err_rhashtable_insert;
return neigh;
err_rhashtable_insert:
kfree(neigh);
err_kzalloc:
return NULL;
}
struct prestera_nh_neigh *
prestera_nh_neigh_find(struct prestera_switch *sw,
struct prestera_nh_neigh_key *key)
{
struct prestera_nh_neigh *nh_neigh;
nh_neigh = rhashtable_lookup_fast(&sw->router->nh_neigh_ht,
key, __prestera_nh_neigh_ht_params);
return IS_ERR(nh_neigh) ? NULL : nh_neigh;
}
struct prestera_nh_neigh *
prestera_nh_neigh_get(struct prestera_switch *sw,
struct prestera_nh_neigh_key *key)
{
struct prestera_nh_neigh *neigh;
neigh = prestera_nh_neigh_find(sw, key);
if (!neigh)
return __prestera_nh_neigh_create(sw, key);
return neigh;
}
void prestera_nh_neigh_put(struct prestera_switch *sw,
struct prestera_nh_neigh *neigh)
{
if (list_empty(&neigh->nexthop_group_list))
__prestera_nh_neigh_destroy(sw, neigh);
}
/* Updates new prestera_neigh_info */
int prestera_nh_neigh_set(struct prestera_switch *sw,
struct prestera_nh_neigh *neigh)
{
struct prestera_nh_neigh_head *nh_head;
struct prestera_nexthop_group *nh_grp;
int err;
list_for_each_entry(nh_head, &neigh->nexthop_group_list, head) {
nh_grp = nh_head->this;
err = prestera_nexthop_group_set(sw, nh_grp);
if (err)
return err;
}
return 0;
}
bool prestera_nh_neigh_util_hw_state(struct prestera_switch *sw,
struct prestera_nh_neigh *nh_neigh)
{
bool state;
struct prestera_nh_neigh_head *nh_head, *tmp;
state = false;
list_for_each_entry_safe(nh_head, tmp,
&nh_neigh->nexthop_group_list, head) {
state = prestera_nexthop_group_util_hw_state(sw, nh_head->this);
if (state)
goto out;
}
out:
return state;
}
static struct prestera_nexthop_group *
__prestera_nexthop_group_create(struct prestera_switch *sw,
struct prestera_nexthop_group_key *key)
{
struct prestera_nexthop_group *nh_grp;
struct prestera_nh_neigh *nh_neigh;
int nh_cnt, err, gid;
nh_grp = kzalloc(sizeof(*nh_grp), GFP_KERNEL);
if (!nh_grp)
goto err_kzalloc;
memcpy(&nh_grp->key, key, sizeof(*key));
for (nh_cnt = 0; nh_cnt < PRESTERA_NHGR_SIZE_MAX; nh_cnt++) {
if (!prestera_nh_neigh_key_is_valid(&nh_grp->key.neigh[nh_cnt]))
break;
nh_neigh = prestera_nh_neigh_get(sw,
&nh_grp->key.neigh[nh_cnt]);
if (!nh_neigh)
goto err_nh_neigh_get;
nh_grp->nh_neigh_head[nh_cnt].neigh = nh_neigh;
nh_grp->nh_neigh_head[nh_cnt].this = nh_grp;
list_add(&nh_grp->nh_neigh_head[nh_cnt].head,
&nh_neigh->nexthop_group_list);
}
err = prestera_hw_nh_group_create(sw, nh_cnt, &nh_grp->grp_id);
if (err)
goto err_nh_group_create;
err = prestera_nexthop_group_set(sw, nh_grp);
if (err)
goto err_nexthop_group_set;
err = rhashtable_insert_fast(&sw->router->nexthop_group_ht,
&nh_grp->ht_node,
__prestera_nexthop_group_ht_params);
if (err)
goto err_ht_insert;
/* reset cache for created group */
gid = nh_grp->grp_id;
sw->router->nhgrp_hw_state_cache[gid / 8] &= ~BIT(gid % 8);
return nh_grp;
err_ht_insert:
err_nexthop_group_set:
prestera_hw_nh_group_delete(sw, nh_cnt, nh_grp->grp_id);
err_nh_group_create:
err_nh_neigh_get:
for (nh_cnt--; nh_cnt >= 0; nh_cnt--) {
list_del(&nh_grp->nh_neigh_head[nh_cnt].head);
prestera_nh_neigh_put(sw, nh_grp->nh_neigh_head[nh_cnt].neigh);
}
kfree(nh_grp);
err_kzalloc:
return NULL;
}
static void
__prestera_nexthop_group_destroy(struct prestera_switch *sw,
struct prestera_nexthop_group *nh_grp)
{
struct prestera_nh_neigh *nh_neigh;
int nh_cnt;
rhashtable_remove_fast(&sw->router->nexthop_group_ht,
&nh_grp->ht_node,
__prestera_nexthop_group_ht_params);
for (nh_cnt = 0; nh_cnt < PRESTERA_NHGR_SIZE_MAX; nh_cnt++) {
nh_neigh = nh_grp->nh_neigh_head[nh_cnt].neigh;
if (!nh_neigh)
break;
list_del(&nh_grp->nh_neigh_head[nh_cnt].head);
prestera_nh_neigh_put(sw, nh_neigh);
}
prestera_hw_nh_group_delete(sw, nh_cnt, nh_grp->grp_id);
kfree(nh_grp);
}
static struct prestera_nexthop_group *
__prestera_nexthop_group_find(struct prestera_switch *sw,
struct prestera_nexthop_group_key *key)
{
struct prestera_nexthop_group *nh_grp;
nh_grp = rhashtable_lookup_fast(&sw->router->nexthop_group_ht,
key, __prestera_nexthop_group_ht_params);
return IS_ERR(nh_grp) ? NULL : nh_grp;
}
static struct prestera_nexthop_group *
prestera_nexthop_group_get(struct prestera_switch *sw,
struct prestera_nexthop_group_key *key)
{
struct prestera_nexthop_group *nh_grp;
nh_grp = __prestera_nexthop_group_find(sw, key);
if (nh_grp) {
refcount_inc(&nh_grp->refcount);
} else {
nh_grp = __prestera_nexthop_group_create(sw, key);
if (IS_ERR(nh_grp))
return ERR_CAST(nh_grp);
refcount_set(&nh_grp->refcount, 1);
}
return nh_grp;
}
static void prestera_nexthop_group_put(struct prestera_switch *sw,
struct prestera_nexthop_group *nh_grp)
{
if (refcount_dec_and_test(&nh_grp->refcount))
__prestera_nexthop_group_destroy(sw, nh_grp);
}
/* Updates with new nh_neigh's info */
static int prestera_nexthop_group_set(struct prestera_switch *sw,
struct prestera_nexthop_group *nh_grp)
{
struct prestera_neigh_info info[PRESTERA_NHGR_SIZE_MAX];
struct prestera_nh_neigh *neigh;
int nh_cnt;
memset(&info[0], 0, sizeof(info));
for (nh_cnt = 0; nh_cnt < PRESTERA_NHGR_SIZE_MAX; nh_cnt++) {
neigh = nh_grp->nh_neigh_head[nh_cnt].neigh;
if (!neigh)
break;
memcpy(&info[nh_cnt], &neigh->info, sizeof(neigh->info));
}
return prestera_hw_nh_entries_set(sw, nh_cnt, &info[0], nh_grp->grp_id);
}
static bool
prestera_nexthop_group_util_hw_state(struct prestera_switch *sw,
struct prestera_nexthop_group *nh_grp)
{
int err;
u32 buf_size = sw->size_tbl_router_nexthop / 8 + 1;
u32 gid = nh_grp->grp_id;
u8 *cache = sw->router->nhgrp_hw_state_cache;
/* Antijitter
* Prevent situation, when we read state of nh_grp twice in short time,
* and state bit is still cleared on second call. So just stuck active
* state for PRESTERA_NH_ACTIVE_JIFFER_FILTER, after last occurred.
*/
if (!time_before(jiffies, sw->router->nhgrp_hw_cache_kick +
msecs_to_jiffies(PRESTERA_NH_ACTIVE_JIFFER_FILTER))) {
err = prestera_hw_nhgrp_blk_get(sw, cache, buf_size);
if (err) {
pr_err("Failed to get hw state nh_grp's");
return false;
}
sw->router->nhgrp_hw_cache_kick = jiffies;
}
if (cache[gid / 8] & BIT(gid % 8))
return true;
return false;
}
struct prestera_fib_node * struct prestera_fib_node *
prestera_fib_node_find(struct prestera_switch *sw, struct prestera_fib_key *key) prestera_fib_node_find(struct prestera_switch *sw, struct prestera_fib_key *key)
{ {
...@@ -251,6 +584,9 @@ static void __prestera_fib_node_destruct(struct prestera_switch *sw, ...@@ -251,6 +584,9 @@ static void __prestera_fib_node_destruct(struct prestera_switch *sw,
prestera_hw_lpm_del(sw, vr->hw_vr_id, fib_node->key.addr.u.ipv4, prestera_hw_lpm_del(sw, vr->hw_vr_id, fib_node->key.addr.u.ipv4,
fib_node->key.prefix_len); fib_node->key.prefix_len);
switch (fib_node->info.type) { switch (fib_node->info.type) {
case PRESTERA_FIB_TYPE_UC_NH:
prestera_nexthop_group_put(sw, fib_node->info.nh_grp);
break;
case PRESTERA_FIB_TYPE_TRAP: case PRESTERA_FIB_TYPE_TRAP:
break; break;
case PRESTERA_FIB_TYPE_DROP: case PRESTERA_FIB_TYPE_DROP:
...@@ -272,10 +608,20 @@ void prestera_fib_node_destroy(struct prestera_switch *sw, ...@@ -272,10 +608,20 @@ void prestera_fib_node_destroy(struct prestera_switch *sw,
kfree(fib_node); kfree(fib_node);
} }
static void prestera_fib_node_destroy_ht_cb(void *ptr, void *arg)
{
struct prestera_fib_node *node = ptr;
struct prestera_switch *sw = arg;
__prestera_fib_node_destruct(sw, node);
kfree(node);
}
struct prestera_fib_node * struct prestera_fib_node *
prestera_fib_node_create(struct prestera_switch *sw, prestera_fib_node_create(struct prestera_switch *sw,
struct prestera_fib_key *key, struct prestera_fib_key *key,
enum prestera_fib_type fib_type) enum prestera_fib_type fib_type,
struct prestera_nexthop_group_key *nh_grp_key)
{ {
struct prestera_fib_node *fib_node; struct prestera_fib_node *fib_node;
u32 grp_id; u32 grp_id;
...@@ -302,6 +648,14 @@ prestera_fib_node_create(struct prestera_switch *sw, ...@@ -302,6 +648,14 @@ prestera_fib_node_create(struct prestera_switch *sw,
case PRESTERA_FIB_TYPE_DROP: case PRESTERA_FIB_TYPE_DROP:
grp_id = PRESTERA_NHGR_DROP; grp_id = PRESTERA_NHGR_DROP;
break; break;
case PRESTERA_FIB_TYPE_UC_NH:
fib_node->info.nh_grp = prestera_nexthop_group_get(sw,
nh_grp_key);
if (!fib_node->info.nh_grp)
goto err_nh_grp_get;
grp_id = fib_node->info.nh_grp->grp_id;
break;
default: default:
pr_err("Unsupported fib_type %d", fib_type); pr_err("Unsupported fib_type %d", fib_type);
goto err_nh_grp_get; goto err_nh_grp_get;
...@@ -323,6 +677,8 @@ prestera_fib_node_create(struct prestera_switch *sw, ...@@ -323,6 +677,8 @@ prestera_fib_node_create(struct prestera_switch *sw,
prestera_hw_lpm_del(sw, vr->hw_vr_id, key->addr.u.ipv4, prestera_hw_lpm_del(sw, vr->hw_vr_id, key->addr.u.ipv4,
key->prefix_len); key->prefix_len);
err_lpm_add: err_lpm_add:
if (fib_type == PRESTERA_FIB_TYPE_UC_NH)
prestera_nexthop_group_put(sw, fib_node->info.nh_grp);
err_nh_grp_get: err_nh_grp_get:
prestera_vr_put(sw, vr); prestera_vr_put(sw, vr);
err_vr_get: err_vr_get:
......
...@@ -31,6 +31,63 @@ struct prestera_ip_addr { ...@@ -31,6 +31,63 @@ struct prestera_ip_addr {
PRESTERA_IPV4 = 0, PRESTERA_IPV4 = 0,
PRESTERA_IPV6 PRESTERA_IPV6
} v; } v;
#define PRESTERA_IP_ADDR_PLEN(V) ((V) == PRESTERA_IPV4 ? 32 : \
/* (V) == PRESTERA_IPV6 ? */ 128 /* : 0 */)
};
struct prestera_nh_neigh_key {
struct prestera_ip_addr addr;
/* Seems like rif is obsolete, because there is iface in info ?
* Key can contain functional fields, or fields, which is used to
* filter duplicate objects on logical level (before you pass it to
* HW)... also key can be used to cover hardware restrictions.
* In our case rif - is logical interface (even can be VLAN), which
* is used in combination with IP address (which is also not related to
* hardware nexthop) to provide logical compression of created nexthops.
* You even can imagine, that rif+IPaddr is just cookie.
*/
/* struct prestera_rif *rif; */
/* Use just as cookie, to divide ARP domains (in order with addr) */
void *rif;
};
/* Used for hw call */
struct prestera_neigh_info {
struct prestera_iface iface;
unsigned char ha[ETH_ALEN];
u8 connected; /* bool. indicate, if mac/oif valid */
u8 __pad[1];
};
/* Used to notify nh about neigh change */
struct prestera_nh_neigh {
struct prestera_nh_neigh_key key;
struct prestera_neigh_info info;
struct rhash_head ht_node; /* node of prestera_vr */
struct list_head nexthop_group_list;
};
#define PRESTERA_NHGR_SIZE_MAX 4
struct prestera_nexthop_group {
struct prestera_nexthop_group_key {
struct prestera_nh_neigh_key neigh[PRESTERA_NHGR_SIZE_MAX];
} key;
/* Store intermediate object here.
* This prevent overhead kzalloc call.
*/
/* nh_neigh is used only to notify nexthop_group */
struct prestera_nh_neigh_head {
struct prestera_nexthop_group *this;
struct list_head head;
/* ptr to neigh is not necessary.
* It used to prevent lookup of nh_neigh by key (n) on destroy
*/
struct prestera_nh_neigh *neigh;
} nh_neigh_head[PRESTERA_NHGR_SIZE_MAX];
struct rhash_head ht_node; /* node of prestera_vr */
refcount_t refcount;
u32 grp_id; /* hw */
}; };
struct prestera_fib_key { struct prestera_fib_key {
...@@ -44,12 +101,16 @@ struct prestera_fib_info { ...@@ -44,12 +101,16 @@ struct prestera_fib_info {
struct list_head vr_node; struct list_head vr_node;
enum prestera_fib_type { enum prestera_fib_type {
PRESTERA_FIB_TYPE_INVALID = 0, PRESTERA_FIB_TYPE_INVALID = 0,
/* must be pointer to nh_grp id */
PRESTERA_FIB_TYPE_UC_NH,
/* It can be connected route /* It can be connected route
* and will be overlapped with neighbours * and will be overlapped with neighbours
*/ */
PRESTERA_FIB_TYPE_TRAP, PRESTERA_FIB_TYPE_TRAP,
PRESTERA_FIB_TYPE_DROP PRESTERA_FIB_TYPE_DROP
} type; } type;
/* Valid only if type = UC_NH*/
struct prestera_nexthop_group *nh_grp;
}; };
struct prestera_fib_node { struct prestera_fib_node {
...@@ -67,6 +128,18 @@ struct prestera_rif_entry * ...@@ -67,6 +128,18 @@ struct prestera_rif_entry *
prestera_rif_entry_create(struct prestera_switch *sw, prestera_rif_entry_create(struct prestera_switch *sw,
struct prestera_rif_entry_key *k, struct prestera_rif_entry_key *k,
u32 tb_id, const unsigned char *addr); u32 tb_id, const unsigned char *addr);
struct prestera_nh_neigh *
prestera_nh_neigh_find(struct prestera_switch *sw,
struct prestera_nh_neigh_key *key);
struct prestera_nh_neigh *
prestera_nh_neigh_get(struct prestera_switch *sw,
struct prestera_nh_neigh_key *key);
void prestera_nh_neigh_put(struct prestera_switch *sw,
struct prestera_nh_neigh *neigh);
int prestera_nh_neigh_set(struct prestera_switch *sw,
struct prestera_nh_neigh *neigh);
bool prestera_nh_neigh_util_hw_state(struct prestera_switch *sw,
struct prestera_nh_neigh *nh_neigh);
struct prestera_fib_node *prestera_fib_node_find(struct prestera_switch *sw, struct prestera_fib_node *prestera_fib_node_find(struct prestera_switch *sw,
struct prestera_fib_key *key); struct prestera_fib_key *key);
void prestera_fib_node_destroy(struct prestera_switch *sw, void prestera_fib_node_destroy(struct prestera_switch *sw,
...@@ -74,7 +147,8 @@ void prestera_fib_node_destroy(struct prestera_switch *sw, ...@@ -74,7 +147,8 @@ void prestera_fib_node_destroy(struct prestera_switch *sw,
struct prestera_fib_node * struct prestera_fib_node *
prestera_fib_node_create(struct prestera_switch *sw, prestera_fib_node_create(struct prestera_switch *sw,
struct prestera_fib_key *key, struct prestera_fib_key *key,
enum prestera_fib_type fib_type); enum prestera_fib_type fib_type,
struct prestera_nexthop_group_key *nh_grp_key);
int prestera_router_hw_init(struct prestera_switch *sw); int prestera_router_hw_init(struct prestera_switch *sw);
void prestera_router_hw_fini(struct prestera_switch *sw); void prestera_router_hw_fini(struct prestera_switch *sw);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment