Skip to content

Commit 7c2ce6e

Browse files
Sujith Sankardavem330
authored andcommitted
enic: Add support for adaptive interrupt coalescing
This patch adds support for adaptive interrupt coalescing. For small pkts with low pkt rate, we can decrease the coalescing interrupt dynamically which decreases the latency. This however increases the cpu utilization. Based on testing with different coal intr and pkt rate we came up with a table(mod_table) with rx_rate and coalescing interrupt value where we get low latency without significant increase in cpu. mod_table table stores the coalescing timer percentage value for different throughputs. Function enic_calc_int_moderation() calculates the desired coalescing intr timer value. This function is called in driver rx napi_poll. The actual value is set by enic_set_int_moderation() which is called when napi_poll is complete. i.e when we unmask the rx intr. Adaptive coal intr is support only when driver is using msix intr. Because intr is not shared. Struct mod_range is used to store only the default adaptive coalescing intr value. Adaptive coal intr calue is calculated by timer = range_start + ((rx_coal->range_end - range_start) * mod_table[index].range_percent / 100); rx_coal->range_end is the rx-usecs-high value set using ethtool. range_start is rx-usecs-low, set using ethtool, if rx_small_pkt_bytes_cnt is greater than 2 * rx_large_pkt_bytes_cnt. i.e small pkts are dominant. Else its rx-usecs-low + 3. Cc: Christian Benvenuti <benve@cisco.com> Cc: Neel Patel <neepatel@cisco.com> Signed-off-by: Sujith Sankar <ssujith@cisco.com> Signed-off-by: Govindarajulu Varadarajan <_govind@gmx.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f6e92d1 commit 7c2ce6e

4 files changed

Lines changed: 244 additions & 6 deletions

File tree

‎drivers/net/ethernet/cisco/enic/enic.h‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,42 @@
4343
#define ENIC_CQ_MAX (ENIC_WQ_MAX + ENIC_RQ_MAX)
4444
#define ENIC_INTR_MAX (ENIC_CQ_MAX + 2)
4545

46+
#define ENIC_AIC_LARGE_PKT_DIFF 3
47+
4648
struct enic_msix_entry {
4749
int requested;
4850
char devname[IFNAMSIZ];
4951
irqreturn_t (*isr)(int, void *);
5052
void *devid;
5153
};
5254

55+
/* Store only the lower range. Higher range is given by fw. */
56+
struct enic_intr_mod_range {
57+
u32 small_pkt_range_start;
58+
u32 large_pkt_range_start;
59+
};
60+
61+
struct enic_intr_mod_table {
62+
u32 rx_rate;
63+
u32 range_percent;
64+
};
65+
66+
#define ENIC_MAX_LINK_SPEEDS 3
67+
#define ENIC_LINK_SPEED_10G 10000
68+
#define ENIC_LINK_SPEED_4G 4000
69+
#define ENIC_LINK_40G_INDEX 2
70+
#define ENIC_LINK_10G_INDEX 1
71+
#define ENIC_LINK_4G_INDEX 0
72+
#define ENIC_RX_COALESCE_RANGE_END 125
73+
#define ENIC_AIC_TS_BREAK 100
74+
75+
struct enic_rx_coal {
76+
u32 small_pkt_range_start;
77+
u32 large_pkt_range_start;
78+
u32 range_end;
79+
u32 use_adaptive_rx_coalesce;
80+
};
81+
5382
/* priv_flags */
5483
#define ENIC_SRIOV_ENABLED (1 << 0)
5584

@@ -92,6 +121,7 @@ struct enic {
92121
unsigned int mc_count;
93122
unsigned int uc_count;
94123
u32 port_mtu;
124+
struct enic_rx_coal rx_coalesce_setting;
95125
u32 rx_coalesce_usecs;
96126
u32 tx_coalesce_usecs;
97127
#ifdef CONFIG_PCI_IOV

‎drivers/net/ethernet/cisco/enic/enic_ethtool.c‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ static const struct enic_stat enic_rx_stats[] = {
7979
static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
8080
static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
8181

82+
void enic_intr_coal_set_rx(struct enic *enic, u32 timer)
83+
{
84+
int i;
85+
int intr;
86+
87+
for (i = 0; i < enic->rq_count; i++) {
88+
intr = enic_msix_rq_intr(enic, i);
89+
vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
90+
}
91+
}
92+
8293
static int enic_get_settings(struct net_device *netdev,
8394
struct ethtool_cmd *ecmd)
8495
{
@@ -178,9 +189,14 @@ static int enic_get_coalesce(struct net_device *netdev,
178189
struct ethtool_coalesce *ecmd)
179190
{
180191
struct enic *enic = netdev_priv(netdev);
192+
struct enic_rx_coal *rxcoal = &enic->rx_coalesce_setting;
181193

182194
ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
183195
ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
196+
if (rxcoal->use_adaptive_rx_coalesce)
197+
ecmd->use_adaptive_rx_coalesce = 1;
198+
ecmd->rx_coalesce_usecs_low = rxcoal->small_pkt_range_start;
199+
ecmd->rx_coalesce_usecs_high = rxcoal->range_end;
184200

185201
return 0;
186202
}
@@ -191,17 +207,31 @@ static int enic_set_coalesce(struct net_device *netdev,
191207
struct enic *enic = netdev_priv(netdev);
192208
u32 tx_coalesce_usecs;
193209
u32 rx_coalesce_usecs;
210+
u32 rx_coalesce_usecs_low;
211+
u32 rx_coalesce_usecs_high;
212+
u32 coalesce_usecs_max;
194213
unsigned int i, intr;
214+
struct enic_rx_coal *rxcoal = &enic->rx_coalesce_setting;
195215

216+
coalesce_usecs_max = vnic_dev_get_intr_coal_timer_max(enic->vdev);
196217
tx_coalesce_usecs = min_t(u32, ecmd->tx_coalesce_usecs,
197-
vnic_dev_get_intr_coal_timer_max(enic->vdev));
218+
coalesce_usecs_max);
198219
rx_coalesce_usecs = min_t(u32, ecmd->rx_coalesce_usecs,
199-
vnic_dev_get_intr_coal_timer_max(enic->vdev));
220+
coalesce_usecs_max);
221+
222+
rx_coalesce_usecs_low = min_t(u32, ecmd->rx_coalesce_usecs_low,
223+
coalesce_usecs_max);
224+
rx_coalesce_usecs_high = min_t(u32, ecmd->rx_coalesce_usecs_high,
225+
coalesce_usecs_max);
200226

201227
switch (vnic_dev_get_intr_mode(enic->vdev)) {
202228
case VNIC_DEV_INTR_MODE_INTX:
203229
if (tx_coalesce_usecs != rx_coalesce_usecs)
204230
return -EINVAL;
231+
if (ecmd->use_adaptive_rx_coalesce ||
232+
ecmd->rx_coalesce_usecs_low ||
233+
ecmd->rx_coalesce_usecs_high)
234+
return -EOPNOTSUPP;
205235

206236
intr = enic_legacy_io_intr();
207237
vnic_intr_coalescing_timer_set(&enic->intr[intr],
@@ -210,6 +240,10 @@ static int enic_set_coalesce(struct net_device *netdev,
210240
case VNIC_DEV_INTR_MODE_MSI:
211241
if (tx_coalesce_usecs != rx_coalesce_usecs)
212242
return -EINVAL;
243+
if (ecmd->use_adaptive_rx_coalesce ||
244+
ecmd->rx_coalesce_usecs_low ||
245+
ecmd->rx_coalesce_usecs_high)
246+
return -EOPNOTSUPP;
213247

214248
vnic_intr_coalescing_timer_set(&enic->intr[0],
215249
tx_coalesce_usecs);
@@ -221,12 +255,27 @@ static int enic_set_coalesce(struct net_device *netdev,
221255
tx_coalesce_usecs);
222256
}
223257

224-
for (i = 0; i < enic->rq_count; i++) {
225-
intr = enic_msix_rq_intr(enic, i);
226-
vnic_intr_coalescing_timer_set(&enic->intr[intr],
227-
rx_coalesce_usecs);
258+
if (rxcoal->use_adaptive_rx_coalesce) {
259+
if (!ecmd->use_adaptive_rx_coalesce) {
260+
rxcoal->use_adaptive_rx_coalesce = 0;
261+
enic_intr_coal_set_rx(enic, rx_coalesce_usecs);
262+
}
263+
} else {
264+
if (ecmd->use_adaptive_rx_coalesce)
265+
rxcoal->use_adaptive_rx_coalesce = 1;
266+
else
267+
enic_intr_coal_set_rx(enic, rx_coalesce_usecs);
228268
}
229269

270+
if (ecmd->rx_coalesce_usecs_high) {
271+
if (rx_coalesce_usecs_high <
272+
(rx_coalesce_usecs_low + ENIC_AIC_LARGE_PKT_DIFF))
273+
return -EINVAL;
274+
rxcoal->range_end = rx_coalesce_usecs_high;
275+
rxcoal->small_pkt_range_start = rx_coalesce_usecs_low;
276+
rxcoal->large_pkt_range_start = rx_coalesce_usecs_low +
277+
ENIC_AIC_LARGE_PKT_DIFF;
278+
}
230279
break;
231280
default:
232281
break;

‎drivers/net/ethernet/cisco/enic/enic_main.c‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <linux/rtnetlink.h>
3939
#include <linux/prefetch.h>
4040
#include <net/ip6_checksum.h>
41+
#include <linux/ktime.h>
4142

4243
#include "cq_enet_desc.h"
4344
#include "vnic_dev.h"
@@ -72,6 +73,35 @@ MODULE_LICENSE("GPL");
7273
MODULE_VERSION(DRV_VERSION);
7374
MODULE_DEVICE_TABLE(pci, enic_id_table);
7475

76+
#define ENIC_LARGE_PKT_THRESHOLD 1000
77+
#define ENIC_MAX_COALESCE_TIMERS 10
78+
/* Interrupt moderation table, which will be used to decide the
79+
* coalescing timer values
80+
* {rx_rate in Mbps, mapping percentage of the range}
81+
*/
82+
struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
83+
{4000, 0},
84+
{4400, 10},
85+
{5060, 20},
86+
{5230, 30},
87+
{5540, 40},
88+
{5820, 50},
89+
{6120, 60},
90+
{6435, 70},
91+
{6745, 80},
92+
{7000, 90},
93+
{0xFFFFFFFF, 100}
94+
};
95+
96+
/* This table helps the driver to pick different ranges for rx coalescing
97+
* timer depending on the link speed.
98+
*/
99+
struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
100+
{0, 0}, /* 0 - 4 Gbps */
101+
{0, 3}, /* 4 - 10 Gbps */
102+
{3, 6}, /* 10 - 40 Gbps */
103+
};
104+
75105
int enic_is_dynamic(struct enic *enic)
76106
{
77107
return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
@@ -979,13 +1009,23 @@ static int enic_rq_alloc_buf(struct vnic_rq *rq)
9791009
return 0;
9801010
}
9811011

1012+
static void enic_intr_update_pkt_size(struct vnic_rx_bytes_counter *pkt_size,
1013+
u32 pkt_len)
1014+
{
1015+
if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len)
1016+
pkt_size->large_pkt_bytes_cnt += pkt_len;
1017+
else
1018+
pkt_size->small_pkt_bytes_cnt += pkt_len;
1019+
}
1020+
9821021
static void enic_rq_indicate_buf(struct vnic_rq *rq,
9831022
struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
9841023
int skipped, void *opaque)
9851024
{
9861025
struct enic *enic = vnic_dev_priv(rq->vdev);
9871026
struct net_device *netdev = enic->netdev;
9881027
struct sk_buff *skb;
1028+
struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
9891029

9901030
u8 type, color, eop, sop, ingress_port, vlan_stripped;
9911031
u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
@@ -1056,6 +1096,9 @@ static void enic_rq_indicate_buf(struct vnic_rq *rq,
10561096
napi_gro_receive(&enic->napi[q_number], skb);
10571097
else
10581098
netif_receive_skb(skb);
1099+
if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1100+
enic_intr_update_pkt_size(&cq->pkt_size_counter,
1101+
bytes_written);
10591102
} else {
10601103

10611104
/* Buffer overflow
@@ -1134,6 +1177,64 @@ static int enic_poll(struct napi_struct *napi, int budget)
11341177
return rq_work_done;
11351178
}
11361179

1180+
static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
1181+
{
1182+
unsigned int intr = enic_msix_rq_intr(enic, rq->index);
1183+
struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1184+
u32 timer = cq->tobe_rx_coal_timeval;
1185+
1186+
if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
1187+
vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
1188+
cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
1189+
}
1190+
}
1191+
1192+
static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
1193+
{
1194+
struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1195+
struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1196+
struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
1197+
int index;
1198+
u32 timer;
1199+
u32 range_start;
1200+
u32 traffic;
1201+
u64 delta;
1202+
ktime_t now = ktime_get();
1203+
1204+
delta = ktime_us_delta(now, cq->prev_ts);
1205+
if (delta < ENIC_AIC_TS_BREAK)
1206+
return;
1207+
cq->prev_ts = now;
1208+
1209+
traffic = pkt_size_counter->large_pkt_bytes_cnt +
1210+
pkt_size_counter->small_pkt_bytes_cnt;
1211+
/* The table takes Mbps
1212+
* traffic *= 8 => bits
1213+
* traffic *= (10^6 / delta) => bps
1214+
* traffic /= 10^6 => Mbps
1215+
*
1216+
* Combining, traffic *= (8 / delta)
1217+
*/
1218+
1219+
traffic <<= 3;
1220+
traffic /= delta;
1221+
1222+
for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
1223+
if (traffic < mod_table[index].rx_rate)
1224+
break;
1225+
range_start = (pkt_size_counter->small_pkt_bytes_cnt >
1226+
pkt_size_counter->large_pkt_bytes_cnt << 1) ?
1227+
rx_coal->small_pkt_range_start :
1228+
rx_coal->large_pkt_range_start;
1229+
timer = range_start + ((rx_coal->range_end - range_start) *
1230+
mod_table[index].range_percent / 100);
1231+
/* Damping */
1232+
cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
1233+
1234+
pkt_size_counter->large_pkt_bytes_cnt = 0;
1235+
pkt_size_counter->small_pkt_bytes_cnt = 0;
1236+
}
1237+
11371238
static int enic_poll_msix(struct napi_struct *napi, int budget)
11381239
{
11391240
struct net_device *netdev = napi->dev;
@@ -1171,6 +1272,13 @@ static int enic_poll_msix(struct napi_struct *napi, int budget)
11711272

11721273
if (err)
11731274
work_done = work_to_do;
1275+
if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1276+
/* Call the function which refreshes
1277+
* the intr coalescing timer value based on
1278+
* the traffic. This is supported only in
1279+
* the case of MSI-x mode
1280+
*/
1281+
enic_calc_int_moderation(enic, &enic->rq[rq]);
11741282

11751283
if (work_done < work_to_do) {
11761284

@@ -1179,6 +1287,8 @@ static int enic_poll_msix(struct napi_struct *napi, int budget)
11791287
*/
11801288

11811289
napi_complete(napi);
1290+
if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1291+
enic_set_int_moderation(enic, &enic->rq[rq]);
11821292
vnic_intr_unmask(&enic->intr[intr]);
11831293
}
11841294

@@ -1314,6 +1424,42 @@ static void enic_synchronize_irqs(struct enic *enic)
13141424
}
13151425
}
13161426

1427+
static void enic_set_rx_coal_setting(struct enic *enic)
1428+
{
1429+
unsigned int speed;
1430+
int index = -1;
1431+
struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1432+
1433+
/* If intr mode is not MSIX, do not do adaptive coalescing */
1434+
if (VNIC_DEV_INTR_MODE_MSIX != vnic_dev_get_intr_mode(enic->vdev)) {
1435+
netdev_info(enic->netdev, "INTR mode is not MSIX, Not initializing adaptive coalescing");
1436+
return;
1437+
}
1438+
1439+
/* 1. Read the link speed from fw
1440+
* 2. Pick the default range for the speed
1441+
* 3. Update it in enic->rx_coalesce_setting
1442+
*/
1443+
speed = vnic_dev_port_speed(enic->vdev);
1444+
if (ENIC_LINK_SPEED_10G < speed)
1445+
index = ENIC_LINK_40G_INDEX;
1446+
else if (ENIC_LINK_SPEED_4G < speed)
1447+
index = ENIC_LINK_10G_INDEX;
1448+
else
1449+
index = ENIC_LINK_4G_INDEX;
1450+
1451+
rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
1452+
rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
1453+
rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
1454+
1455+
/* Start with the value provided by UCSM */
1456+
for (index = 0; index < enic->rq_count; index++)
1457+
enic->cq[index].cur_rx_coal_timeval =
1458+
enic->config.intr_timer_usec;
1459+
1460+
rx_coal->use_adaptive_rx_coalesce = 1;
1461+
}
1462+
13171463
static int enic_dev_notify_set(struct enic *enic)
13181464
{
13191465
int err;
@@ -2231,6 +2377,7 @@ static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
22312377
enic->notify_timer.function = enic_notify_timer;
22322378
enic->notify_timer.data = (unsigned long)enic;
22332379

2380+
enic_set_rx_coal_setting(enic);
22342381
INIT_WORK(&enic->reset, enic_reset);
22352382
INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
22362383

@@ -2250,6 +2397,9 @@ static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
22502397
}
22512398

22522399
enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2400+
/* rx coalesce time already got initialized. This gets used
2401+
* if adaptive coal is turned off
2402+
*/
22532403
enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
22542404

22552405
if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))

0 commit comments

Comments
 (0)