Skip to content

Commit e0c0afd

Browse files
Arkadi Sharshevskydavem330
authored andcommitted
mlxsw: spectrum: Support for counters on router interfaces
Add support for counter allocation on router interfaces. The allocation depends on the counter state of relevant table. In case the counting is disabled or no counters left the counter index will be set as invalid. Also a counter pool for router allocation is added. Signed-off-by: Arakdi Sharshevsky <arkadis@mellanox.com> Signed-off-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ba73e97 commit e0c0afd

5 files changed

Lines changed: 232 additions & 0 deletions

File tree

‎drivers/net/ethernet/mellanox/mlxsw/resources.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum mlxsw_res_id {
4646
MLXSW_RES_ID_COUNTER_POOL_SIZE,
4747
MLXSW_RES_ID_MAX_SPAN,
4848
MLXSW_RES_ID_COUNTER_SIZE_PACKETS_BYTES,
49+
MLXSW_RES_ID_COUNTER_SIZE_ROUTER_BASIC,
4950
MLXSW_RES_ID_MAX_SYSTEM_PORT,
5051
MLXSW_RES_ID_MAX_LAG,
5152
MLXSW_RES_ID_MAX_LAG_MEMBERS,
@@ -82,6 +83,7 @@ static u16 mlxsw_res_ids[] = {
8283
[MLXSW_RES_ID_COUNTER_POOL_SIZE] = 0x2410,
8384
[MLXSW_RES_ID_MAX_SPAN] = 0x2420,
8485
[MLXSW_RES_ID_COUNTER_SIZE_PACKETS_BYTES] = 0x2443,
86+
[MLXSW_RES_ID_COUNTER_SIZE_ROUTER_BASIC] = 0x2449,
8587
[MLXSW_RES_ID_MAX_SYSTEM_PORT] = 0x2502,
8688
[MLXSW_RES_ID_MAX_LAG] = 0x2520,
8789
[MLXSW_RES_ID_MAX_LAG_MEMBERS] = 0x2521,

‎drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ static struct mlxsw_sp_counter_sub_pool mlxsw_sp_counter_sub_pools[] = {
5656
[MLXSW_SP_COUNTER_SUB_POOL_FLOW] = {
5757
.bank_count = 6,
5858
},
59+
[MLXSW_SP_COUNTER_SUB_POOL_RIF] = {
60+
.bank_count = 2,
61+
}
5962
};
6063

6164
static int mlxsw_sp_counter_pool_validate(struct mlxsw_sp *mlxsw_sp)
@@ -83,6 +86,12 @@ static int mlxsw_sp_counter_sub_pools_prepare(struct mlxsw_sp *mlxsw_sp)
8386
return -EIO;
8487
sub_pool->entry_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
8588
COUNTER_SIZE_PACKETS_BYTES);
89+
/* Prepare erif pool*/
90+
sub_pool = &mlxsw_sp_counter_sub_pools[MLXSW_SP_COUNTER_SUB_POOL_RIF];
91+
if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_SIZE_ROUTER_BASIC))
92+
return -EIO;
93+
sub_pool->entry_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
94+
COUNTER_SIZE_ROUTER_BASIC);
8695
return 0;
8796
}
8897

‎drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
enum mlxsw_sp_counter_sub_pool_id {
4141
MLXSW_SP_COUNTER_SUB_POOL_FLOW,
42+
MLXSW_SP_COUNTER_SUB_POOL_RIF,
4243
};
4344

4445
int mlxsw_sp_counter_alloc(struct mlxsw_sp *mlxsw_sp,

‎drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
#include "spectrum.h"
5353
#include "core.h"
5454
#include "reg.h"
55+
#include "spectrum_cnt.h"
56+
#include "spectrum_dpipe.h"
57+
#include "spectrum_router.h"
5558

5659
struct mlxsw_sp_rif {
5760
struct list_head nexthop_list;
@@ -62,8 +65,157 @@ struct mlxsw_sp_rif {
6265
int mtu;
6366
u16 rif_index;
6467
u16 vr_id;
68+
unsigned int counter_ingress;
69+
bool counter_ingress_valid;
70+
unsigned int counter_egress;
71+
bool counter_egress_valid;
6572
};
6673

74+
static unsigned int *
75+
mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
76+
enum mlxsw_sp_rif_counter_dir dir)
77+
{
78+
switch (dir) {
79+
case MLXSW_SP_RIF_COUNTER_EGRESS:
80+
return &rif->counter_egress;
81+
case MLXSW_SP_RIF_COUNTER_INGRESS:
82+
return &rif->counter_ingress;
83+
}
84+
return NULL;
85+
}
86+
87+
static bool
88+
mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
89+
enum mlxsw_sp_rif_counter_dir dir)
90+
{
91+
switch (dir) {
92+
case MLXSW_SP_RIF_COUNTER_EGRESS:
93+
return rif->counter_egress_valid;
94+
case MLXSW_SP_RIF_COUNTER_INGRESS:
95+
return rif->counter_ingress_valid;
96+
}
97+
return false;
98+
}
99+
100+
static void
101+
mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
102+
enum mlxsw_sp_rif_counter_dir dir,
103+
bool valid)
104+
{
105+
switch (dir) {
106+
case MLXSW_SP_RIF_COUNTER_EGRESS:
107+
rif->counter_egress_valid = valid;
108+
break;
109+
case MLXSW_SP_RIF_COUNTER_INGRESS:
110+
rif->counter_ingress_valid = valid;
111+
break;
112+
}
113+
}
114+
115+
static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
116+
unsigned int counter_index, bool enable,
117+
enum mlxsw_sp_rif_counter_dir dir)
118+
{
119+
char ritr_pl[MLXSW_REG_RITR_LEN];
120+
bool is_egress = false;
121+
int err;
122+
123+
if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
124+
is_egress = true;
125+
mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
126+
err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
127+
if (err)
128+
return err;
129+
130+
mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
131+
is_egress);
132+
return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
133+
}
134+
135+
int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
136+
struct mlxsw_sp_rif *rif,
137+
enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
138+
{
139+
char ricnt_pl[MLXSW_REG_RICNT_LEN];
140+
unsigned int *p_counter_index;
141+
bool valid;
142+
int err;
143+
144+
valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
145+
if (!valid)
146+
return -EINVAL;
147+
148+
p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
149+
if (!p_counter_index)
150+
return -EINVAL;
151+
mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
152+
MLXSW_REG_RICNT_OPCODE_NOP);
153+
err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
154+
if (err)
155+
return err;
156+
*cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
157+
return 0;
158+
}
159+
160+
static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
161+
unsigned int counter_index)
162+
{
163+
char ricnt_pl[MLXSW_REG_RICNT_LEN];
164+
165+
mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
166+
MLXSW_REG_RICNT_OPCODE_CLEAR);
167+
return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
168+
}
169+
170+
int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
171+
struct mlxsw_sp_rif *rif,
172+
enum mlxsw_sp_rif_counter_dir dir)
173+
{
174+
unsigned int *p_counter_index;
175+
int err;
176+
177+
p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
178+
if (!p_counter_index)
179+
return -EINVAL;
180+
err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
181+
p_counter_index);
182+
if (err)
183+
return err;
184+
185+
err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
186+
if (err)
187+
goto err_counter_clear;
188+
189+
err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
190+
*p_counter_index, true, dir);
191+
if (err)
192+
goto err_counter_edit;
193+
mlxsw_sp_rif_counter_valid_set(rif, dir, true);
194+
return 0;
195+
196+
err_counter_edit:
197+
err_counter_clear:
198+
mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
199+
*p_counter_index);
200+
return err;
201+
}
202+
203+
void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
204+
struct mlxsw_sp_rif *rif,
205+
enum mlxsw_sp_rif_counter_dir dir)
206+
{
207+
unsigned int *p_counter_index;
208+
209+
p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
210+
if (WARN_ON(!p_counter_index))
211+
return;
212+
mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
213+
*p_counter_index, false, dir);
214+
mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
215+
*p_counter_index);
216+
mlxsw_sp_rif_counter_valid_set(rif, dir, false);
217+
}
218+
67219
static struct mlxsw_sp_rif *
68220
mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
69221
const struct net_device *dev);
@@ -2822,6 +2974,15 @@ mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
28222974
goto err_rif_alloc;
28232975
}
28242976

2977+
if (devlink_dpipe_table_counter_enabled(priv_to_devlink(mlxsw_sp->core),
2978+
MLXSW_SP_DPIPE_TABLE_NAME_ERIF)) {
2979+
err = mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif,
2980+
MLXSW_SP_RIF_COUNTER_EGRESS);
2981+
if (err)
2982+
netdev_dbg(mlxsw_sp_vport->dev,
2983+
"Counter alloc Failed err=%d\n", err);
2984+
}
2985+
28252986
f->rif = rif;
28262987
mlxsw_sp->rifs[rif_index] = rif;
28272988
vr->rif_count++;
@@ -2852,6 +3013,9 @@ static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
28523013

28533014
mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
28543015

3016+
mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
3017+
mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_INGRESS);
3018+
28553019
vr->rif_count--;
28563020
mlxsw_sp->rifs[rif_index] = NULL;
28573021
f->rif = NULL;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* drivers/net/ethernet/mellanox/mlxsw/spectrum_router.h
3+
* Copyright (c) 2017 Mellanox Technologies. All rights reserved.
4+
* Copyright (c) 2017 Arkadi Sharshevsky <arkadis@mellanox.com>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* 3. Neither the names of the copyright holders nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* Alternatively, this software may be distributed under the terms of the
19+
* GNU General Public License ("GPL") version 2 as published by the Free
20+
* Software Foundation.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
#ifndef _MLXSW_ROUTER_H_
36+
#define _MLXSW_ROUTER_H_
37+
38+
#include "spectrum.h"
39+
40+
enum mlxsw_sp_rif_counter_dir {
41+
MLXSW_SP_RIF_COUNTER_INGRESS,
42+
MLXSW_SP_RIF_COUNTER_EGRESS,
43+
};
44+
45+
int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
46+
struct mlxsw_sp_rif *rif,
47+
enum mlxsw_sp_rif_counter_dir dir,
48+
u64 *cnt);
49+
void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
50+
struct mlxsw_sp_rif *rif,
51+
enum mlxsw_sp_rif_counter_dir dir);
52+
int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
53+
struct mlxsw_sp_rif *rif,
54+
enum mlxsw_sp_rif_counter_dir dir);
55+
56+
#endif /* _MLXSW_ROUTER_H_*/

0 commit comments

Comments
 (0)