1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2019-2021 Marvell International Ltd. All rights reserved */
4 #include <linux/rhashtable.h>
7 #include "prestera_hw.h"
8 #include "prestera_router_hw.h"
9 #include "prestera_acl.h"
15 * +-+-------+ +--+---+-+
16 * |rif_entry| |fib_node|
17 * +---------+ +--------+
18 * Rif is Fib - is exit point
24 #define PRESTERA_NHGR_UNUSED (0)
25 #define PRESTERA_NHGR_DROP (0xFFFFFFFF)
27 static const struct rhashtable_params __prestera_fib_ht_params = {
28 .key_offset = offsetof(struct prestera_fib_node, key),
29 .head_offset = offsetof(struct prestera_fib_node, ht_node),
30 .key_len = sizeof(struct prestera_fib_key),
31 .automatic_shrinking = true,
34 int prestera_router_hw_init(struct prestera_switch *sw)
38 err = rhashtable_init(&sw->router->fib_ht,
39 &__prestera_fib_ht_params);
43 INIT_LIST_HEAD(&sw->router->vr_list);
44 INIT_LIST_HEAD(&sw->router->rif_entry_list);
50 void prestera_router_hw_fini(struct prestera_switch *sw)
52 WARN_ON(!list_empty(&sw->router->vr_list));
53 WARN_ON(!list_empty(&sw->router->rif_entry_list));
54 rhashtable_destroy(&sw->router->fib_ht);
57 static struct prestera_vr *__prestera_vr_find(struct prestera_switch *sw,
60 struct prestera_vr *vr;
62 list_for_each_entry(vr, &sw->router->vr_list, router_node) {
63 if (vr->tb_id == tb_id)
70 static struct prestera_vr *__prestera_vr_create(struct prestera_switch *sw,
72 struct netlink_ext_ack *extack)
74 struct prestera_vr *vr;
77 vr = kzalloc(sizeof(*vr), GFP_KERNEL);
85 err = prestera_hw_vr_create(sw, &vr->hw_vr_id);
89 list_add(&vr->router_node, &sw->router->vr_list);
99 static void __prestera_vr_destroy(struct prestera_switch *sw,
100 struct prestera_vr *vr)
102 list_del(&vr->router_node);
103 prestera_hw_vr_delete(sw, vr->hw_vr_id);
107 static struct prestera_vr *prestera_vr_get(struct prestera_switch *sw, u32 tb_id,
108 struct netlink_ext_ack *extack)
110 struct prestera_vr *vr;
112 vr = __prestera_vr_find(sw, tb_id);
114 refcount_inc(&vr->refcount);
116 vr = __prestera_vr_create(sw, tb_id, extack);
120 refcount_set(&vr->refcount, 1);
126 static void prestera_vr_put(struct prestera_switch *sw, struct prestera_vr *vr)
128 if (refcount_dec_and_test(&vr->refcount))
129 __prestera_vr_destroy(sw, vr);
132 /* iface is overhead struct. vr_id also can be removed. */
134 __prestera_rif_entry_key_copy(const struct prestera_rif_entry_key *in,
135 struct prestera_rif_entry_key *out)
137 memset(out, 0, sizeof(*out));
139 switch (in->iface.type) {
140 case PRESTERA_IF_PORT_E:
141 out->iface.dev_port.hw_dev_num = in->iface.dev_port.hw_dev_num;
142 out->iface.dev_port.port_num = in->iface.dev_port.port_num;
144 case PRESTERA_IF_LAG_E:
145 out->iface.lag_id = in->iface.lag_id;
147 case PRESTERA_IF_VID_E:
148 out->iface.vlan_id = in->iface.vlan_id;
151 WARN(1, "Unsupported iface type");
155 out->iface.type = in->iface.type;
159 struct prestera_rif_entry *
160 prestera_rif_entry_find(const struct prestera_switch *sw,
161 const struct prestera_rif_entry_key *k)
163 struct prestera_rif_entry *rif_entry;
164 struct prestera_rif_entry_key lk; /* lookup key */
166 if (__prestera_rif_entry_key_copy(k, &lk))
169 list_for_each_entry(rif_entry, &sw->router->rif_entry_list,
171 if (!memcmp(k, &rif_entry->key, sizeof(*k)))
178 void prestera_rif_entry_destroy(struct prestera_switch *sw,
179 struct prestera_rif_entry *e)
181 struct prestera_iface iface;
183 list_del(&e->router_node);
185 memcpy(&iface, &e->key.iface, sizeof(iface));
186 iface.vr_id = e->vr->hw_vr_id;
187 prestera_hw_rif_delete(sw, e->hw_id, &iface);
189 prestera_vr_put(sw, e->vr);
193 struct prestera_rif_entry *
194 prestera_rif_entry_create(struct prestera_switch *sw,
195 struct prestera_rif_entry_key *k,
196 u32 tb_id, const unsigned char *addr)
199 struct prestera_rif_entry *e;
200 struct prestera_iface iface;
202 e = kzalloc(sizeof(*e), GFP_KERNEL);
206 if (__prestera_rif_entry_key_copy(k, &e->key))
209 e->vr = prestera_vr_get(sw, tb_id, NULL);
213 memcpy(&e->addr, addr, sizeof(e->addr));
216 memcpy(&iface, &e->key.iface, sizeof(iface));
217 iface.vr_id = e->vr->hw_vr_id;
218 err = prestera_hw_rif_create(sw, &iface, e->addr, &e->hw_id);
222 list_add(&e->router_node, &sw->router->rif_entry_list);
227 prestera_vr_put(sw, e->vr);
235 struct prestera_fib_node *
236 prestera_fib_node_find(struct prestera_switch *sw, struct prestera_fib_key *key)
238 struct prestera_fib_node *fib_node;
240 fib_node = rhashtable_lookup_fast(&sw->router->fib_ht, key,
241 __prestera_fib_ht_params);
245 static void __prestera_fib_node_destruct(struct prestera_switch *sw,
246 struct prestera_fib_node *fib_node)
248 struct prestera_vr *vr;
250 vr = fib_node->info.vr;
251 prestera_hw_lpm_del(sw, vr->hw_vr_id, fib_node->key.addr.u.ipv4,
252 fib_node->key.prefix_len);
253 switch (fib_node->info.type) {
254 case PRESTERA_FIB_TYPE_TRAP:
256 case PRESTERA_FIB_TYPE_DROP:
259 pr_err("Unknown fib_node->info.type = %d",
260 fib_node->info.type);
263 prestera_vr_put(sw, vr);
266 void prestera_fib_node_destroy(struct prestera_switch *sw,
267 struct prestera_fib_node *fib_node)
269 __prestera_fib_node_destruct(sw, fib_node);
270 rhashtable_remove_fast(&sw->router->fib_ht, &fib_node->ht_node,
271 __prestera_fib_ht_params);
275 struct prestera_fib_node *
276 prestera_fib_node_create(struct prestera_switch *sw,
277 struct prestera_fib_key *key,
278 enum prestera_fib_type fib_type)
280 struct prestera_fib_node *fib_node;
282 struct prestera_vr *vr;
285 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
289 memcpy(&fib_node->key, key, sizeof(*key));
290 fib_node->info.type = fib_type;
292 vr = prestera_vr_get(sw, key->tb_id, NULL);
296 fib_node->info.vr = vr;
299 case PRESTERA_FIB_TYPE_TRAP:
300 grp_id = PRESTERA_NHGR_UNUSED;
302 case PRESTERA_FIB_TYPE_DROP:
303 grp_id = PRESTERA_NHGR_DROP;
306 pr_err("Unsupported fib_type %d", fib_type);
310 err = prestera_hw_lpm_add(sw, vr->hw_vr_id, key->addr.u.ipv4,
311 key->prefix_len, grp_id);
315 err = rhashtable_insert_fast(&sw->router->fib_ht, &fib_node->ht_node,
316 __prestera_fib_ht_params);
323 prestera_hw_lpm_del(sw, vr->hw_vr_id, key->addr.u.ipv4,
327 prestera_vr_put(sw, vr);