Skip to content

Commit 75cabb4

Browse files
Erni Sri Satya VennelaPaolo Abeni
authored andcommitted
net: mana: Add support for net_shaper_ops
Introduce support for net_shaper_ops in the MANA driver, enabling configuration of rate limiting on the MANA NIC. To apply rate limiting, the driver issues a HWC command via mana_set_bw_clamp() and updates the corresponding shaper object in the net_shaper cache. If an error occurs during this process, the driver restores the previous speed by querying the current link configuration using mana_query_link_cfg(). The minimum supported bandwidth is 100 Mbps, and only values that are exact multiples of 100 Mbps are allowed. Any other values are rejected. To remove a shaper, the driver resets the bandwidth to the maximum supported by the SKU using mana_set_bw_clamp() and clears the associated cache entry. If an error occurs during this process, the shaper details are retained. On the hardware that does not support these APIs, the net-shaper calls to set speed would fail. Set the speed: ./tools/net/ynl/pyynl/cli.py \ --spec Documentation/netlink/specs/net_shaper.yaml \ --do set --json '{"ifindex":'$IFINDEX', "handle":{"scope": "netdev", "id":'$ID' }, "bw-max": 200000000 }' Get the shaper details: ./tools/net/ynl/pyynl/cli.py \ --spec Documentation/netlink/specs/net_shaper.yaml \ --do get --json '{"ifindex":'$IFINDEX', "handle":{"scope": "netdev", "id":'$ID' }}' > {'bw-max': 200000000, > 'handle': {'scope': 'netdev'}, > 'ifindex': $IFINDEX, > 'metric': 'bps'} Delete the shaper object: ./tools/net/ynl/pyynl/cli.py \ --spec Documentation/netlink/specs/net_shaper.yaml \ --do delete --json '{"ifindex":'$IFINDEX', "handle":{"scope": "netdev","id":'$ID' }}' Signed-off-by: Erni Sri Satya Vennela <[email protected]> Reviewed-by: Haiyang Zhang <[email protected]> Reviewed-by: Shradha Gupta <[email protected]> Reviewed-by: Saurabh Singh Sengar <[email protected]> Reviewed-by: Long Li <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent d5c8f0e commit 75cabb4

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

drivers/net/ethernet/microsoft/mana/mana_en.c

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,78 @@ static int mana_change_mtu(struct net_device *ndev, int new_mtu)
719719
return err;
720720
}
721721

722+
static int mana_shaper_set(struct net_shaper_binding *binding,
723+
const struct net_shaper *shaper,
724+
struct netlink_ext_ack *extack)
725+
{
726+
struct mana_port_context *apc = netdev_priv(binding->netdev);
727+
u32 old_speed, rate;
728+
int err;
729+
730+
if (shaper->handle.scope != NET_SHAPER_SCOPE_NETDEV) {
731+
NL_SET_ERR_MSG_MOD(extack, "net shaper scope should be netdev");
732+
return -EINVAL;
733+
}
734+
735+
if (apc->handle.id && shaper->handle.id != apc->handle.id) {
736+
NL_SET_ERR_MSG_MOD(extack, "Cannot create multiple shapers");
737+
return -EOPNOTSUPP;
738+
}
739+
740+
if (!shaper->bw_max || (shaper->bw_max % 100000000)) {
741+
NL_SET_ERR_MSG_MOD(extack, "Please use multiples of 100Mbps for bandwidth");
742+
return -EINVAL;
743+
}
744+
745+
rate = div_u64(shaper->bw_max, 1000); /* Convert bps to Kbps */
746+
rate = div_u64(rate, 1000); /* Convert Kbps to Mbps */
747+
748+
/* Get current speed */
749+
err = mana_query_link_cfg(apc);
750+
old_speed = (err) ? SPEED_UNKNOWN : apc->speed;
751+
752+
if (!err) {
753+
err = mana_set_bw_clamp(apc, rate, TRI_STATE_TRUE);
754+
apc->speed = (err) ? old_speed : rate;
755+
apc->handle = (err) ? apc->handle : shaper->handle;
756+
}
757+
758+
return err;
759+
}
760+
761+
static int mana_shaper_del(struct net_shaper_binding *binding,
762+
const struct net_shaper_handle *handle,
763+
struct netlink_ext_ack *extack)
764+
{
765+
struct mana_port_context *apc = netdev_priv(binding->netdev);
766+
int err;
767+
768+
err = mana_set_bw_clamp(apc, 0, TRI_STATE_FALSE);
769+
770+
if (!err) {
771+
/* Reset mana port context parameters */
772+
apc->handle.id = 0;
773+
apc->handle.scope = NET_SHAPER_SCOPE_UNSPEC;
774+
apc->speed = 0;
775+
}
776+
777+
return err;
778+
}
779+
780+
static void mana_shaper_cap(struct net_shaper_binding *binding,
781+
enum net_shaper_scope scope,
782+
unsigned long *flags)
783+
{
784+
*flags = BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX) |
785+
BIT(NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS);
786+
}
787+
788+
static const struct net_shaper_ops mana_shaper_ops = {
789+
.set = mana_shaper_set,
790+
.delete = mana_shaper_del,
791+
.capabilities = mana_shaper_cap,
792+
};
793+
722794
static const struct net_device_ops mana_devops = {
723795
.ndo_open = mana_open,
724796
.ndo_stop = mana_close,
@@ -729,6 +801,7 @@ static const struct net_device_ops mana_devops = {
729801
.ndo_bpf = mana_bpf,
730802
.ndo_xdp_xmit = mana_xdp_xmit,
731803
.ndo_change_mtu = mana_change_mtu,
804+
.net_shaper_ops = &mana_shaper_ops,
732805
};
733806

734807
static void mana_cleanup_port_context(struct mana_port_context *apc)
@@ -1162,6 +1235,86 @@ static int mana_cfg_vport_steering(struct mana_port_context *apc,
11621235
return err;
11631236
}
11641237

1238+
int mana_query_link_cfg(struct mana_port_context *apc)
1239+
{
1240+
struct net_device *ndev = apc->ndev;
1241+
struct mana_query_link_config_resp resp = {};
1242+
struct mana_query_link_config_req req = {};
1243+
int err;
1244+
1245+
mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_LINK_CONFIG,
1246+
sizeof(req), sizeof(resp));
1247+
1248+
req.vport = apc->port_handle;
1249+
req.hdr.resp.msg_version = GDMA_MESSAGE_V2;
1250+
1251+
err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1252+
sizeof(resp));
1253+
1254+
if (err) {
1255+
netdev_err(ndev, "Failed to query link config: %d\n", err);
1256+
return err;
1257+
}
1258+
1259+
err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_LINK_CONFIG,
1260+
sizeof(resp));
1261+
1262+
if (err || resp.hdr.status) {
1263+
netdev_err(ndev, "Failed to query link config: %d, 0x%x\n", err,
1264+
resp.hdr.status);
1265+
if (!err)
1266+
err = -EOPNOTSUPP;
1267+
return err;
1268+
}
1269+
1270+
if (resp.qos_unconfigured) {
1271+
err = -EINVAL;
1272+
return err;
1273+
}
1274+
apc->speed = resp.link_speed_mbps;
1275+
return 0;
1276+
}
1277+
1278+
int mana_set_bw_clamp(struct mana_port_context *apc, u32 speed,
1279+
int enable_clamping)
1280+
{
1281+
struct mana_set_bw_clamp_resp resp = {};
1282+
struct mana_set_bw_clamp_req req = {};
1283+
struct net_device *ndev = apc->ndev;
1284+
int err;
1285+
1286+
mana_gd_init_req_hdr(&req.hdr, MANA_SET_BW_CLAMP,
1287+
sizeof(req), sizeof(resp));
1288+
req.vport = apc->port_handle;
1289+
req.link_speed_mbps = speed;
1290+
req.enable_clamping = enable_clamping;
1291+
1292+
err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1293+
sizeof(resp));
1294+
1295+
if (err) {
1296+
netdev_err(ndev, "Failed to set bandwidth clamp for speed %u, err = %d",
1297+
speed, err);
1298+
return err;
1299+
}
1300+
1301+
err = mana_verify_resp_hdr(&resp.hdr, MANA_SET_BW_CLAMP,
1302+
sizeof(resp));
1303+
1304+
if (err || resp.hdr.status) {
1305+
netdev_err(ndev, "Failed to set bandwidth clamp: %d, 0x%x\n", err,
1306+
resp.hdr.status);
1307+
if (!err)
1308+
err = -EOPNOTSUPP;
1309+
return err;
1310+
}
1311+
1312+
if (resp.qos_unconfigured)
1313+
netdev_info(ndev, "QoS is unconfigured\n");
1314+
1315+
return 0;
1316+
}
1317+
11651318
int mana_create_wq_obj(struct mana_port_context *apc,
11661319
mana_handle_t vport,
11671320
u32 wq_type, struct mana_obj_spec *wq_spec,
@@ -3011,6 +3164,8 @@ static int mana_probe_port(struct mana_context *ac, int port_idx,
30113164
goto free_indir;
30123165
}
30133166

3167+
debugfs_create_u32("current_speed", 0400, apc->mana_port_debugfs, &apc->speed);
3168+
30143169
return 0;
30153170

30163171
free_indir:

include/net/mana/mana.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define _MANA_H
66

77
#include <net/xdp.h>
8+
#include <net/net_shaper.h>
89

910
#include "gdma.h"
1011
#include "hw_channel.h"
@@ -526,7 +527,12 @@ struct mana_port_context {
526527
struct mutex vport_mutex;
527528
int vport_use_count;
528529

530+
/* Net shaper handle*/
531+
struct net_shaper_handle handle;
532+
529533
u16 port_idx;
534+
/* Currently configured speed (mbps) */
535+
u32 speed;
530536

531537
bool port_is_up;
532538
bool port_st_save; /* Saved port state */
@@ -562,6 +568,9 @@ struct bpf_prog *mana_xdp_get(struct mana_port_context *apc);
562568
void mana_chn_setxdp(struct mana_port_context *apc, struct bpf_prog *prog);
563569
int mana_bpf(struct net_device *ndev, struct netdev_bpf *bpf);
564570
void mana_query_gf_stats(struct mana_port_context *apc);
571+
int mana_query_link_cfg(struct mana_port_context *apc);
572+
int mana_set_bw_clamp(struct mana_port_context *apc, u32 speed,
573+
int enable_clamping);
565574
void mana_query_phy_stats(struct mana_port_context *apc);
566575
int mana_pre_alloc_rxbufs(struct mana_port_context *apc, int mtu, int num_queues);
567576
void mana_pre_dealloc_rxbufs(struct mana_port_context *apc);
@@ -589,6 +598,8 @@ enum mana_command_code {
589598
MANA_FENCE_RQ = 0x20006,
590599
MANA_CONFIG_VPORT_RX = 0x20007,
591600
MANA_QUERY_VPORT_CONFIG = 0x20008,
601+
MANA_QUERY_LINK_CONFIG = 0x2000A,
602+
MANA_SET_BW_CLAMP = 0x2000B,
592603
MANA_QUERY_PHY_STAT = 0x2000c,
593604

594605
/* Privileged commands for the PF mode */
@@ -598,6 +609,35 @@ enum mana_command_code {
598609
MANA_DEREGISTER_HW_PORT = 0x28004,
599610
};
600611

612+
/* Query Link Configuration*/
613+
struct mana_query_link_config_req {
614+
struct gdma_req_hdr hdr;
615+
mana_handle_t vport;
616+
}; /* HW DATA */
617+
618+
struct mana_query_link_config_resp {
619+
struct gdma_resp_hdr hdr;
620+
u32 qos_speed_mbps;
621+
u8 qos_unconfigured;
622+
u8 reserved1[3];
623+
u32 link_speed_mbps;
624+
u8 reserved2[4];
625+
}; /* HW DATA */
626+
627+
/* Set Bandwidth Clamp*/
628+
struct mana_set_bw_clamp_req {
629+
struct gdma_req_hdr hdr;
630+
mana_handle_t vport;
631+
enum TRI_STATE enable_clamping;
632+
u32 link_speed_mbps;
633+
}; /* HW DATA */
634+
635+
struct mana_set_bw_clamp_resp {
636+
struct gdma_resp_hdr hdr;
637+
u8 qos_unconfigured;
638+
u8 reserved[7];
639+
}; /* HW DATA */
640+
601641
/* Query Device Configuration */
602642
struct mana_query_device_cfg_req {
603643
struct gdma_req_hdr hdr;

0 commit comments

Comments
 (0)