Skip to content

Commit a5ea31f

Browse files
committed
Merge branch 'net-offloaded-stats'
Jiri Pirko says: ==================== net: return offloaded stats as default and expose original sw stats The problem we try to handle is about offloaded forwarded packets which are not seen by kernel. Let me try to draw it: port1 port2 (HW stats are counted here) \ / \ / \ / --(A)---- ASIC --(B)-- | (C) | CPU (SW stats are counted here) Now we have couple of flows for TX and RX (direction does not matter here): 1) port1->A->ASIC->C->CPU For this flow, HW and SW stats are equal. 2) port1->A->ASIC->C->CPU->C->ASIC->B->port2 For this flow, HW and SW stats are equal. 3) port1->A->ASIC->B->port2 For this flow, SW stats are 0. The purpose of this patchset is to provide facility for user to find out the difference between flows 1+2 and 3. In other words, user will be able to see the statistics for the slow-path (through kernel). Also note that HW stats are what someone calls "accumulated" stats. Every packet counted by SW is also counted by HW. Not the other way around. As a default the accumulated stats (HW) will be exposed to user so the userspace apps can react properly. This patchset add the SW stats (flows 1+2) under offload related stats, so in the future we can expose other offload related stat in a similar way. --- v9->v10: - patch 2/3 - removed unnecessary ()s as pointed out by Nik v8->v9: - patch 2/3 - add using of idxattr and prividx v7->v8: - patch 2/3 - move helping const from uapi to rtnetlink - cancel driver xstat nesting if it is empty v6->v7: - patch 1/3: - ndo interface changed to get the wanted stats type as an input. - change commit message. - patch 2/3: - create a nesting for offloaded stat and put SW stats under it. - change the ndo call to indicate which offload stats we wants. - change commit message. - patch 3/3: - change ndo implementation to match the changes in the previous patches. - change commit message. v5->v6: - patch 2/4 was dropped as requested by Roopa - patch 1/3: - comment changed to indicate that default stats are combined stats - commit massage changed - patch 2/3: (previously 3/4) - SW stats return nothing if there is no SW stats ndo v4->v5: - updated cover letter - patch3/4: - using memcpy directly to copy stats as requested by DaveM v3->v4: - patch1/4: - fixed "return ()" pointed out by EricD - patch2/4: - fixed if_nlmsg_size as pointed out by EricD v2->v3: - patch1/4: - added dev_have_sw_stats helper - patch2/4: - avoided memcpy as requested by DaveM - patch3/4: - use new dev_have_sw_stats helper v1->v2: - patch3/4: - fixed NULL initialization ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents c13ed53 + fc1bbb0 commit a5ea31f

5 files changed

Lines changed: 255 additions & 11 deletions

File tree

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

Lines changed: 122 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,9 @@ static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
819819
return err;
820820
}
821821

822-
static struct rtnl_link_stats64 *
823-
mlxsw_sp_port_get_stats64(struct net_device *dev,
824-
struct rtnl_link_stats64 *stats)
822+
int
823+
mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
824+
struct rtnl_link_stats64 *stats)
825825
{
826826
struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
827827
struct mlxsw_sp_port_pcpu_stats *p;
@@ -848,6 +848,107 @@ mlxsw_sp_port_get_stats64(struct net_device *dev,
848848
tx_dropped += p->tx_dropped;
849849
}
850850
stats->tx_dropped = tx_dropped;
851+
return 0;
852+
}
853+
854+
bool mlxsw_sp_port_has_offload_stats(int attr_id)
855+
{
856+
switch (attr_id) {
857+
case IFLA_OFFLOAD_XSTATS_CPU_HIT:
858+
return true;
859+
}
860+
861+
return false;
862+
}
863+
864+
int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
865+
void *sp)
866+
{
867+
switch (attr_id) {
868+
case IFLA_OFFLOAD_XSTATS_CPU_HIT:
869+
return mlxsw_sp_port_get_sw_stats64(dev, sp);
870+
}
871+
872+
return -EINVAL;
873+
}
874+
875+
static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
876+
int prio, char *ppcnt_pl)
877+
{
878+
struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
879+
struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
880+
881+
mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
882+
return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
883+
}
884+
885+
static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
886+
struct rtnl_link_stats64 *stats)
887+
{
888+
char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
889+
int err;
890+
891+
err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
892+
0, ppcnt_pl);
893+
if (err)
894+
goto out;
895+
896+
stats->tx_packets =
897+
mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
898+
stats->rx_packets =
899+
mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
900+
stats->tx_bytes =
901+
mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
902+
stats->rx_bytes =
903+
mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
904+
stats->multicast =
905+
mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
906+
907+
stats->rx_crc_errors =
908+
mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
909+
stats->rx_frame_errors =
910+
mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
911+
912+
stats->rx_length_errors = (
913+
mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
914+
mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
915+
mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
916+
917+
stats->rx_errors = (stats->rx_crc_errors +
918+
stats->rx_frame_errors + stats->rx_length_errors);
919+
920+
out:
921+
return err;
922+
}
923+
924+
static void update_stats_cache(struct work_struct *work)
925+
{
926+
struct mlxsw_sp_port *mlxsw_sp_port =
927+
container_of(work, struct mlxsw_sp_port,
928+
hw_stats.update_dw.work);
929+
930+
if (!netif_carrier_ok(mlxsw_sp_port->dev))
931+
goto out;
932+
933+
mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
934+
mlxsw_sp_port->hw_stats.cache);
935+
936+
out:
937+
mlxsw_core_schedule_dw(&mlxsw_sp_port->hw_stats.update_dw,
938+
MLXSW_HW_STATS_UPDATE_TIME);
939+
}
940+
941+
/* Return the stats from a cache that is updated periodically,
942+
* as this function might get called in an atomic context.
943+
*/
944+
static struct rtnl_link_stats64 *
945+
mlxsw_sp_port_get_stats64(struct net_device *dev,
946+
struct rtnl_link_stats64 *stats)
947+
{
948+
struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
949+
950+
memcpy(stats, mlxsw_sp_port->hw_stats.cache, sizeof(*stats));
951+
851952
return stats;
852953
}
853954

@@ -1209,6 +1310,8 @@ static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
12091310
.ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
12101311
.ndo_change_mtu = mlxsw_sp_port_change_mtu,
12111312
.ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1313+
.ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1314+
.ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
12121315
.ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
12131316
.ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
12141317
.ndo_neigh_construct = mlxsw_sp_router_neigh_construct,
@@ -1547,8 +1650,6 @@ static void __mlxsw_sp_port_get_stats(struct net_device *dev,
15471650
enum mlxsw_reg_ppcnt_grp grp, int prio,
15481651
u64 *data, int data_index)
15491652
{
1550-
struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1551-
struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
15521653
struct mlxsw_sp_port_hw_stats *hw_stats;
15531654
char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
15541655
int i, len;
@@ -1557,8 +1658,7 @@ static void __mlxsw_sp_port_get_stats(struct net_device *dev,
15571658
err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
15581659
if (err)
15591660
return;
1560-
mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
1561-
err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1661+
mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
15621662
for (i = 0; i < len; i++)
15631663
data[data_index + i] = !err ? hw_stats[i].getter(ppcnt_pl) : 0;
15641664
}
@@ -2145,6 +2245,16 @@ static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
21452245
goto err_alloc_stats;
21462246
}
21472247

2248+
mlxsw_sp_port->hw_stats.cache =
2249+
kzalloc(sizeof(*mlxsw_sp_port->hw_stats.cache), GFP_KERNEL);
2250+
2251+
if (!mlxsw_sp_port->hw_stats.cache) {
2252+
err = -ENOMEM;
2253+
goto err_alloc_hw_stats;
2254+
}
2255+
INIT_DELAYED_WORK(&mlxsw_sp_port->hw_stats.update_dw,
2256+
&update_stats_cache);
2257+
21482258
dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
21492259
dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
21502260

@@ -2245,6 +2355,7 @@ static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
22452355
goto err_core_port_init;
22462356
}
22472357

2358+
mlxsw_core_schedule_dw(&mlxsw_sp_port->hw_stats.update_dw, 0);
22482359
return 0;
22492360

22502361
err_core_port_init:
@@ -2265,6 +2376,8 @@ static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
22652376
err_dev_addr_init:
22662377
mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
22672378
err_port_swid_set:
2379+
kfree(mlxsw_sp_port->hw_stats.cache);
2380+
err_alloc_hw_stats:
22682381
free_percpu(mlxsw_sp_port->pcpu_stats);
22692382
err_alloc_stats:
22702383
kfree(mlxsw_sp_port->untagged_vlans);
@@ -2281,6 +2394,7 @@ static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
22812394

22822395
if (!mlxsw_sp_port)
22832396
return;
2397+
cancel_delayed_work_sync(&mlxsw_sp_port->hw_stats.update_dw);
22842398
mlxsw_core_port_fini(&mlxsw_sp_port->core_port);
22852399
unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
22862400
mlxsw_sp->ports[local_port] = NULL;
@@ -2290,6 +2404,7 @@ static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
22902404
mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
22912405
mlxsw_sp_port_module_unmap(mlxsw_sp, mlxsw_sp_port->local_port);
22922406
free_percpu(mlxsw_sp_port->pcpu_stats);
2407+
kfree(mlxsw_sp_port->hw_stats.cache);
22932408
kfree(mlxsw_sp_port->untagged_vlans);
22942409
kfree(mlxsw_sp_port->active_vlans);
22952410
WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vports_list));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ struct mlxsw_sp_port {
361361
struct list_head vports_list;
362362
/* TC handles */
363363
struct list_head mall_tc_list;
364+
struct {
365+
#define MLXSW_HW_STATS_UPDATE_TIME HZ
366+
struct rtnl_link_stats64 *cache;
367+
struct delayed_work update_dw;
368+
} hw_stats;
364369
};
365370

366371
struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev);

‎include/linux/netdevice.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,14 @@ struct netdev_xdp {
924924
* 3. Update dev->stats asynchronously and atomically, and define
925925
* neither operation.
926926
*
927+
* bool (*ndo_has_offload_stats)(int attr_id)
928+
* Return true if this device supports offload stats of this attr_id.
929+
*
930+
* int (*ndo_get_offload_stats)(int attr_id, const struct net_device *dev,
931+
* void *attr_data)
932+
* Get statistics for offload operations by attr_id. Write it into the
933+
* attr_data pointer.
934+
*
927935
* int (*ndo_vlan_rx_add_vid)(struct net_device *dev, __be16 proto, u16 vid);
928936
* If device supports VLAN filtering this function is called when a
929937
* VLAN id is registered.
@@ -1155,6 +1163,10 @@ struct net_device_ops {
11551163

11561164
struct rtnl_link_stats64* (*ndo_get_stats64)(struct net_device *dev,
11571165
struct rtnl_link_stats64 *storage);
1166+
bool (*ndo_has_offload_stats)(int attr_id);
1167+
int (*ndo_get_offload_stats)(int attr_id,
1168+
const struct net_device *dev,
1169+
void *attr_data);
11581170
struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
11591171

11601172
int (*ndo_vlan_rx_add_vid)(struct net_device *dev,

‎include/uapi/linux/if_link.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ enum {
826826
IFLA_STATS_LINK_64,
827827
IFLA_STATS_LINK_XSTATS,
828828
IFLA_STATS_LINK_XSTATS_SLAVE,
829+
IFLA_STATS_LINK_OFFLOAD_XSTATS,
829830
__IFLA_STATS_MAX,
830831
};
831832

@@ -845,6 +846,14 @@ enum {
845846
};
846847
#define LINK_XSTATS_TYPE_MAX (__LINK_XSTATS_TYPE_MAX - 1)
847848

849+
/* These are stats embedded into IFLA_STATS_LINK_OFFLOAD_XSTATS */
850+
enum {
851+
IFLA_OFFLOAD_XSTATS_UNSPEC,
852+
IFLA_OFFLOAD_XSTATS_CPU_HIT, /* struct rtnl_link_stats64 */
853+
__IFLA_OFFLOAD_XSTATS_MAX
854+
};
855+
#define IFLA_OFFLOAD_XSTATS_MAX (__IFLA_OFFLOAD_XSTATS_MAX - 1)
856+
848857
/* XDP section */
849858

850859
enum {

0 commit comments

Comments
 (0)