* [net-next, PATCH 0/2, v1] net: socionext: add AF_XDP support
@ 2018-09-10 8:24 Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 1/2, v1] net: socionext: different approach on DMA Ilias Apalodimas
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Ilias Apalodimas @ 2018-09-10 8:24 UTC (permalink / raw)
To: netdev, jaswinder.singh
Cc: ard.biesheuvel, masami.hiramatsu, arnd, mykyta.iziumtsev,
bjorn.topel, magnus.karlsson, Ilias Apalodimas
This patch series adds AF_XDP support socionext netsec driver
- patch [1/2]: Use a different allocation scheme for Rx DMA buffers to prepare
the driver for AF_XDP support
- patch [2/2]: Add AF_XDP support without zero-copy
Ilias Apalodimas (2):
net: socionext: different approach on DMA
net: socionext: add AF_XDP support
drivers/net/ethernet/socionext/netsec.c | 444 +++++++++++++++++++++++---------
1 file changed, 329 insertions(+), 115 deletions(-)
--
2.7.4
^ permalink raw reply [flat|nested] 8+ messages in thread
* [net-next, PATCH 1/2, v1] net: socionext: different approach on DMA
2018-09-10 8:24 [net-next, PATCH 0/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
@ 2018-09-10 8:24 ` Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
2018-09-10 10:06 ` [net-next, PATCH 0/2, " Björn Töpel
2 siblings, 0 replies; 8+ messages in thread
From: Ilias Apalodimas @ 2018-09-10 8:24 UTC (permalink / raw)
To: netdev, jaswinder.singh
Cc: ard.biesheuvel, masami.hiramatsu, arnd, mykyta.iziumtsev,
bjorn.topel, magnus.karlsson, Ilias Apalodimas
Current driver dynamically allocates an skb and maps it as DMA rx buffer.
A following patch introduces AF_XDP functionality, so we need a
different allocation scheme. Buffers are allocated dynamically and
mapped into hardware. During the Rx operation the driver uses
build_skb() to produce the necessary buffers for the network stack
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
drivers/net/ethernet/socionext/netsec.c | 239 +++++++++++++++++---------------
1 file changed, 130 insertions(+), 109 deletions(-)
diff --git a/drivers/net/ethernet/socionext/netsec.c b/drivers/net/ethernet/socionext/netsec.c
index 7aa5ebb..666fee2 100644
--- a/drivers/net/ethernet/socionext/netsec.c
+++ b/drivers/net/ethernet/socionext/netsec.c
@@ -296,6 +296,11 @@ struct netsec_rx_pkt_info {
bool err_flag;
};
+static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num);
+
+static void *netsec_alloc_rx_data(struct netsec_priv *priv,
+ dma_addr_t *dma_addr, u16 *len);
+
static void netsec_write(struct netsec_priv *priv, u32 reg_addr, u32 val)
{
writel(val, priv->ioaddr + reg_addr);
@@ -556,34 +561,10 @@ static const struct ethtool_ops netsec_ethtool_ops = {
/************* NETDEV_OPS FOLLOW *************/
-static struct sk_buff *netsec_alloc_skb(struct netsec_priv *priv,
- struct netsec_desc *desc)
-{
- struct sk_buff *skb;
-
- if (device_get_dma_attr(priv->dev) == DEV_DMA_COHERENT) {
- skb = netdev_alloc_skb_ip_align(priv->ndev, desc->len);
- } else {
- desc->len = L1_CACHE_ALIGN(desc->len);
- skb = netdev_alloc_skb(priv->ndev, desc->len);
- }
- if (!skb)
- return NULL;
-
- desc->addr = skb->data;
- desc->dma_addr = dma_map_single(priv->dev, desc->addr, desc->len,
- DMA_FROM_DEVICE);
- if (dma_mapping_error(priv->dev, desc->dma_addr)) {
- dev_kfree_skb_any(skb);
- return NULL;
- }
- return skb;
-}
static void netsec_set_rx_de(struct netsec_priv *priv,
struct netsec_desc_ring *dring, u16 idx,
- const struct netsec_desc *desc,
- struct sk_buff *skb)
+ const struct netsec_desc *desc)
{
struct netsec_de *de = dring->vaddr + DESC_SZ * idx;
u32 attr = (1 << NETSEC_RX_PKT_OWN_FIELD) |
@@ -602,59 +583,6 @@ static void netsec_set_rx_de(struct netsec_priv *priv,
dring->desc[idx].dma_addr = desc->dma_addr;
dring->desc[idx].addr = desc->addr;
dring->desc[idx].len = desc->len;
- dring->desc[idx].skb = skb;
-}
-
-static struct sk_buff *netsec_get_rx_de(struct netsec_priv *priv,
- struct netsec_desc_ring *dring,
- u16 idx,
- struct netsec_rx_pkt_info *rxpi,
- struct netsec_desc *desc, u16 *len)
-{
- struct netsec_de de = {};
-
- memcpy(&de, dring->vaddr + DESC_SZ * idx, DESC_SZ);
-
- *len = de.buf_len_info >> 16;
-
- rxpi->err_flag = (de.attr >> NETSEC_RX_PKT_ER_FIELD) & 1;
- rxpi->rx_cksum_result = (de.attr >> NETSEC_RX_PKT_CO_FIELD) & 3;
- rxpi->err_code = (de.attr >> NETSEC_RX_PKT_ERR_FIELD) &
- NETSEC_RX_PKT_ERR_MASK;
- *desc = dring->desc[idx];
- return desc->skb;
-}
-
-static struct sk_buff *netsec_get_rx_pkt_data(struct netsec_priv *priv,
- struct netsec_rx_pkt_info *rxpi,
- struct netsec_desc *desc,
- u16 *len)
-{
- struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
- struct sk_buff *tmp_skb, *skb = NULL;
- struct netsec_desc td;
- int tail;
-
- *rxpi = (struct netsec_rx_pkt_info){};
-
- td.len = priv->ndev->mtu + 22;
-
- tmp_skb = netsec_alloc_skb(priv, &td);
-
- tail = dring->tail;
-
- if (!tmp_skb) {
- netsec_set_rx_de(priv, dring, tail, &dring->desc[tail],
- dring->desc[tail].skb);
- } else {
- skb = netsec_get_rx_de(priv, dring, tail, rxpi, desc, len);
- netsec_set_rx_de(priv, dring, tail, &td, tmp_skb);
- }
-
- /* move tail ahead */
- dring->tail = (dring->tail + 1) % DESC_NUM;
-
- return skb;
}
static int netsec_clean_tx_dring(struct netsec_priv *priv, int budget)
@@ -721,19 +649,29 @@ static int netsec_process_tx(struct netsec_priv *priv, int budget)
return done;
}
+static void nsetsec_adv_desc(u16 *idx)
+{
+ *idx = *idx + 1;
+ if (unlikely(*idx >= DESC_NUM))
+ *idx = 0;
+}
+
static int netsec_process_rx(struct netsec_priv *priv, int budget)
{
struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
struct net_device *ndev = priv->ndev;
- struct netsec_rx_pkt_info rx_info;
- int done = 0;
- struct netsec_desc desc;
struct sk_buff *skb;
- u16 len;
+ int done = 0;
while (done < budget) {
u16 idx = dring->tail;
struct netsec_de *de = dring->vaddr + (DESC_SZ * idx);
+ struct netsec_desc *desc = &dring->desc[idx];
+ struct netsec_rx_pkt_info rpi;
+ dma_addr_t dma_handle;
+ void *buf_addr;
+ u16 pkt_len;
+ u16 desc_len;
if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD))
break;
@@ -744,28 +682,62 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
*/
dma_rmb();
done++;
- skb = netsec_get_rx_pkt_data(priv, &rx_info, &desc, &len);
- if (unlikely(!skb) || rx_info.err_flag) {
+
+ pkt_len = de->buf_len_info >> 16;
+ rpi.err_code = (de->attr >> NETSEC_RX_PKT_ERR_FIELD) &
+ NETSEC_RX_PKT_ERR_MASK;
+ rpi.err_flag = (de->attr >> NETSEC_RX_PKT_ER_FIELD) & 1;
+ if (rpi.err_flag) {
netif_err(priv, drv, priv->ndev,
- "%s: rx fail err(%d)\n",
- __func__, rx_info.err_code);
+ "%s: rx fail err(%d)\n", __func__,
+ rpi.err_code);
ndev->stats.rx_dropped++;
+ nsetsec_adv_desc(&dring->tail);
+ /* reuse buffer page frag */
+ netsec_rx_fill(priv, idx, 1);
continue;
}
+ rpi.rx_cksum_result = (de->attr >> NETSEC_RX_PKT_CO_FIELD) & 3;
- dma_unmap_single(priv->dev, desc.dma_addr, desc.len,
- DMA_FROM_DEVICE);
- skb_put(skb, len);
+ dma_sync_single_for_cpu(priv->dev, desc->dma_addr, pkt_len,
+ DMA_FROM_DEVICE);
+
+ prefetch(desc->addr);
+ buf_addr = netsec_alloc_rx_data(priv, &dma_handle, &desc_len);
+ if (unlikely(!buf_addr))
+ break;
+
+ skb = build_skb(desc->addr, desc->len);
+ if (unlikely(!skb)) {
+ dma_unmap_single(priv->dev, dma_handle, desc_len,
+ DMA_TO_DEVICE);
+ skb_free_frag(buf_addr);
+ netif_err(priv, drv, priv->ndev,
+ "rx failed to alloc skb\n");
+ break;
+ }
+ dma_unmap_single_attrs(priv->dev, desc->dma_addr, desc->len,
+ DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
+
+ /* Update the descriptor with fresh buffers */
+ desc->len = desc_len;
+ desc->dma_addr = dma_handle;
+ desc->addr = buf_addr;
+
+ skb_put(skb, pkt_len);
skb->protocol = eth_type_trans(skb, priv->ndev);
if (priv->rx_cksum_offload_flag &&
- rx_info.rx_cksum_result == NETSEC_RX_CKSUM_OK)
+ rpi.rx_cksum_result == NETSEC_RX_CKSUM_OK)
skb->ip_summed = CHECKSUM_UNNECESSARY;
if (napi_gro_receive(&priv->napi, skb) != GRO_DROP) {
ndev->stats.rx_packets++;
- ndev->stats.rx_bytes += len;
+ ndev->stats.rx_bytes += pkt_len;
}
+
+ netsec_rx_fill(priv, idx, 1);
+ nsetsec_adv_desc(&dring->tail);
}
return done;
@@ -928,7 +900,10 @@ static void netsec_uninit_pkt_dring(struct netsec_priv *priv, int id)
dma_unmap_single(priv->dev, desc->dma_addr, desc->len,
id == NETSEC_RING_RX ? DMA_FROM_DEVICE :
DMA_TO_DEVICE);
- dev_kfree_skb(desc->skb);
+ if (id == NETSEC_RING_RX)
+ skb_free_frag(desc->addr);
+ else if (id == NETSEC_RING_TX)
+ dev_kfree_skb(desc->skb);
}
memset(dring->desc, 0, sizeof(struct netsec_desc) * DESC_NUM);
@@ -953,50 +928,96 @@ static void netsec_free_dring(struct netsec_priv *priv, int id)
dring->desc = NULL;
}
+static void *netsec_alloc_rx_data(struct netsec_priv *priv,
+ dma_addr_t *dma_handle, u16 *desc_len)
+{
+ size_t len = priv->ndev->mtu + ETH_HLEN + VLAN_HLEN * 2 + NET_SKB_PAD +
+ NET_IP_ALIGN;
+ dma_addr_t mapping;
+ void *buf;
+
+ len = SKB_DATA_ALIGN(len);
+ len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
+
+ buf = napi_alloc_frag(len);
+ if (!buf)
+ return NULL;
+
+ mapping = dma_map_single(priv->dev, buf, len, DMA_FROM_DEVICE);
+ if (unlikely(dma_mapping_error(priv->dev, mapping)))
+ goto err_out;
+
+ *dma_handle = mapping;
+ *desc_len = len;
+
+ return buf;
+
+err_out:
+ skb_free_frag(buf);
+ return NULL;
+}
+
+static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num)
+{
+ struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
+ u16 idx = from;
+
+ while (num) {
+ netsec_set_rx_de(priv, dring, idx, &dring->desc[idx]);
+ idx++;
+ if (idx >= DESC_NUM)
+ idx = 0;
+ num--;
+ }
+}
+
static int netsec_alloc_dring(struct netsec_priv *priv, enum ring_id id)
{
struct netsec_desc_ring *dring = &priv->desc_ring[id];
- int ret = 0;
dring->vaddr = dma_zalloc_coherent(priv->dev, DESC_SZ * DESC_NUM,
&dring->desc_dma, GFP_KERNEL);
- if (!dring->vaddr) {
- ret = -ENOMEM;
+ if (!dring->vaddr)
goto err;
- }
dring->desc = kcalloc(DESC_NUM, sizeof(*dring->desc), GFP_KERNEL);
- if (!dring->desc) {
- ret = -ENOMEM;
+ if (!dring->desc)
goto err;
- }
return 0;
err:
netsec_free_dring(priv, id);
- return ret;
+ return -ENOMEM;
}
static int netsec_setup_rx_dring(struct netsec_priv *priv)
{
struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
- struct netsec_desc desc;
- struct sk_buff *skb;
- int n;
+ int i;
- desc.len = priv->ndev->mtu + 22;
+ for (i = 0; i < DESC_NUM; i++) {
+ struct netsec_desc *desc = &dring->desc[i];
+ dma_addr_t dma_handle;
+ void *buf;
+ u16 len;
- for (n = 0; n < DESC_NUM; n++) {
- skb = netsec_alloc_skb(priv, &desc);
- if (!skb) {
+ buf = netsec_alloc_rx_data(priv, &dma_handle, &len);
+ if (!buf) {
netsec_uninit_pkt_dring(priv, NETSEC_RING_RX);
- return -ENOMEM;
+ goto err_out;
}
- netsec_set_rx_de(priv, dring, n, &desc, skb);
+ desc->dma_addr = dma_handle;
+ desc->addr = buf;
+ desc->len = len;
}
+ netsec_rx_fill(priv, 0, DESC_NUM);
+
return 0;
+
+err_out:
+ return -ENOMEM;
}
static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg,
--
2.7.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support
2018-09-10 8:24 [net-next, PATCH 0/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 1/2, v1] net: socionext: different approach on DMA Ilias Apalodimas
@ 2018-09-10 8:24 ` Ilias Apalodimas
2018-09-10 10:56 ` Toshiaki Makita
2018-09-10 10:06 ` [net-next, PATCH 0/2, " Björn Töpel
2 siblings, 1 reply; 8+ messages in thread
From: Ilias Apalodimas @ 2018-09-10 8:24 UTC (permalink / raw)
To: netdev, jaswinder.singh
Cc: ard.biesheuvel, masami.hiramatsu, arnd, mykyta.iziumtsev,
bjorn.topel, magnus.karlsson, Ilias Apalodimas
Add basic AF_XDP support without zero-copy
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
drivers/net/ethernet/socionext/netsec.c | 211 ++++++++++++++++++++++++++++++--
1 file changed, 202 insertions(+), 9 deletions(-)
diff --git a/drivers/net/ethernet/socionext/netsec.c b/drivers/net/ethernet/socionext/netsec.c
index 666fee2..7464ca6 100644
--- a/drivers/net/ethernet/socionext/netsec.c
+++ b/drivers/net/ethernet/socionext/netsec.c
@@ -9,6 +9,8 @@
#include <linux/etherdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
+#include <linux/bpf.h>
+#include <linux/bpf_trace.h>
#include <net/tcp.h>
#include <net/ip6_checksum.h>
@@ -238,6 +240,11 @@
#define NETSEC_F_NETSEC_VER_MAJOR_NUM(x) ((x) & 0xffff0000)
+#define NETSEC_XDP_PASS 0
+#define NETSEC_XDP_CONSUMED BIT(0)
+#define NETSEC_XDP_TX BIT(1)
+#define NETSEC_XDP_REDIR BIT(2)
+
enum ring_id {
NETSEC_RING_TX = 0,
NETSEC_RING_RX
@@ -256,11 +263,13 @@ struct netsec_desc_ring {
void *vaddr;
u16 pkt_cnt;
u16 head, tail;
+ struct xdp_rxq_info xdp_rxq;
};
struct netsec_priv {
struct netsec_desc_ring desc_ring[NETSEC_RING_MAX];
struct ethtool_coalesce et_coalesce;
+ struct bpf_prog *xdp_prog;
spinlock_t reglock; /* protect reg access */
struct napi_struct napi;
phy_interface_t phy_interface;
@@ -297,6 +306,8 @@ struct netsec_rx_pkt_info {
};
static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num);
+static u32 netsec_run_xdp(struct netsec_desc *desc, struct netsec_priv *priv,
+ struct bpf_prog *prog, u16 len);
static void *netsec_alloc_rx_data(struct netsec_priv *priv,
dma_addr_t *dma_addr, u16 *len);
@@ -613,13 +624,23 @@ static int netsec_clean_tx_dring(struct netsec_priv *priv, int budget)
eop = (entry->attr >> NETSEC_TX_LAST) & 1;
- dma_unmap_single(priv->dev, desc->dma_addr, desc->len,
- DMA_TO_DEVICE);
- if (eop) {
- pkts++;
+ if (desc->skb) {
+ dma_unmap_single(priv->dev, desc->dma_addr, desc->len,
+ DMA_TO_DEVICE);
+ }
+
+ if (!eop) {
+ *desc = (struct netsec_desc){};
+ continue;
+ }
+
+ if (!desc->skb) {
+ skb_free_frag(desc->addr);
+ } else {
bytes += desc->skb->len;
dev_kfree_skb(desc->skb);
}
+ pkts++;
*desc = (struct netsec_desc){};
}
dring->pkt_cnt -= budget;
@@ -659,8 +680,11 @@ static void nsetsec_adv_desc(u16 *idx)
static int netsec_process_rx(struct netsec_priv *priv, int budget)
{
struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
+ struct bpf_prog *xdp_prog = READ_ONCE(priv->xdp_prog);
struct net_device *ndev = priv->ndev;
- struct sk_buff *skb;
+ struct sk_buff *skb = NULL;
+ u32 xdp_flush = 0;
+ u32 xdp_result;
int done = 0;
while (done < budget) {
@@ -707,6 +731,26 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
if (unlikely(!buf_addr))
break;
+ if (xdp_prog) {
+ xdp_result = netsec_run_xdp(desc, priv, xdp_prog,
+ pkt_len);
+ if (xdp_result != NETSEC_XDP_PASS) {
+ xdp_flush |= xdp_result & NETSEC_XDP_REDIR;
+
+ dma_unmap_single_attrs(priv->dev,
+ desc->dma_addr,
+ desc->len, DMA_TO_DEVICE,
+ DMA_ATTR_SKIP_CPU_SYNC);
+
+ desc->len = desc_len;
+ desc->dma_addr = dma_handle;
+ desc->addr = buf_addr;
+ netsec_rx_fill(priv, idx, 1);
+ nsetsec_adv_desc(&dring->tail);
+ }
+ continue;
+ }
+
skb = build_skb(desc->addr, desc->len);
if (unlikely(!skb)) {
dma_unmap_single(priv->dev, dma_handle, desc_len,
@@ -740,6 +784,9 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
nsetsec_adv_desc(&dring->tail);
}
+ if (xdp_flush & NETSEC_XDP_REDIR)
+ xdp_do_flush_map();
+
return done;
}
@@ -892,6 +939,9 @@ static void netsec_uninit_pkt_dring(struct netsec_priv *priv, int id)
if (!dring->vaddr || !dring->desc)
return;
+ if (xdp_rxq_info_is_reg(&dring->xdp_rxq))
+ xdp_rxq_info_unreg(&dring->xdp_rxq);
+
for (idx = 0; idx < DESC_NUM; idx++) {
desc = &dring->desc[idx];
if (!desc->addr)
@@ -994,7 +1044,7 @@ static int netsec_alloc_dring(struct netsec_priv *priv, enum ring_id id)
static int netsec_setup_rx_dring(struct netsec_priv *priv)
{
struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
- int i;
+ int i, err;
for (i = 0; i < DESC_NUM; i++) {
struct netsec_desc *desc = &dring->desc[i];
@@ -1003,20 +1053,29 @@ static int netsec_setup_rx_dring(struct netsec_priv *priv)
u16 len;
buf = netsec_alloc_rx_data(priv, &dma_handle, &len);
- if (!buf) {
- netsec_uninit_pkt_dring(priv, NETSEC_RING_RX);
+ if (!buf)
goto err_out;
- }
desc->dma_addr = dma_handle;
desc->addr = buf;
desc->len = len;
}
netsec_rx_fill(priv, 0, DESC_NUM);
+ err = xdp_rxq_info_reg(&dring->xdp_rxq, priv->ndev, 0);
+ if (err)
+ goto err_out;
+
+ err = xdp_rxq_info_reg_mem_model(&dring->xdp_rxq, MEM_TYPE_PAGE_SHARED,
+ NULL);
+ if (err) {
+ xdp_rxq_info_unreg(&dring->xdp_rxq);
+ goto err_out;
+ }
return 0;
err_out:
+ netsec_uninit_pkt_dring(priv, NETSEC_RING_RX);
return -ENOMEM;
}
@@ -1353,6 +1412,9 @@ static int netsec_netdev_stop(struct net_device *ndev)
napi_disable(&priv->napi);
+ if (priv->xdp_prog)
+ bpf_prog_put(priv->xdp_prog);
+
netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0);
netsec_stop_gmac(priv);
@@ -1420,6 +1482,136 @@ static int netsec_netdev_ioctl(struct net_device *ndev, struct ifreq *ifr,
return phy_mii_ioctl(ndev->phydev, ifr, cmd);
}
+static u32 netsec_xmit_xdp(struct netsec_priv *priv, struct xdp_buff *xdp,
+ struct netsec_desc *rx_desc)
+{
+ struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX];
+ struct netsec_tx_pkt_ctrl tx_ctrl = {};
+ struct netsec_desc tx_desc;
+ int filled;
+ u32 len;
+
+ len = xdp->data_end - xdp->data;
+
+ if (tx_ring->head >= tx_ring->tail)
+ filled = tx_ring->head - tx_ring->tail;
+ else
+ filled = tx_ring->head + DESC_NUM - tx_ring->tail;
+
+ if (DESC_NUM - filled <= 1)
+ return NETSEC_XDP_CONSUMED;
+
+ dma_sync_single_for_device(priv->dev, rx_desc->dma_addr, len,
+ DMA_TO_DEVICE);
+
+ tx_desc.dma_addr = rx_desc->dma_addr;
+ tx_desc.addr = xdp->data;
+ tx_desc.len = len;
+
+ netsec_set_tx_de(priv, tx_ring, &tx_ctrl, &tx_desc, NULL);
+ netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, 1); /* submit another tx */
+
+ return NETSEC_XDP_TX;
+}
+
+static u32 netsec_run_xdp(struct netsec_desc *desc, struct netsec_priv *priv,
+ struct bpf_prog *prog, u16 len)
+
+{
+ struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
+ struct xdp_buff xdp;
+ u32 ret = NETSEC_XDP_PASS;
+ int err;
+ u32 act;
+
+ xdp.data_hard_start = desc->addr;
+ xdp.data = desc->addr;
+ xdp_set_data_meta_invalid(&xdp);
+ xdp.data_end = xdp.data + len;
+ xdp.rxq = &dring->xdp_rxq;
+
+ rcu_read_lock();
+ act = bpf_prog_run_xdp(prog, &xdp);
+
+ switch (act) {
+ case XDP_PASS:
+ ret = NETSEC_XDP_PASS;
+ break;
+ case XDP_TX:
+ ret = netsec_xmit_xdp(priv, &xdp, desc);
+ break;
+ case XDP_REDIRECT:
+ err = xdp_do_redirect(priv->ndev, &xdp, prog);
+ if (!err) {
+ ret = NETSEC_XDP_REDIR;
+ } else {
+ ret = NETSEC_XDP_CONSUMED;
+ xdp_return_buff(&xdp);
+ }
+ break;
+ default:
+ bpf_warn_invalid_xdp_action(act);
+ /* fall through */
+ case XDP_ABORTED:
+ trace_xdp_exception(priv->ndev, prog, act);
+ /* fall through -- handle aborts by dropping packet */
+ case XDP_DROP:
+ ret = NETSEC_XDP_CONSUMED;
+ break;
+ }
+
+ rcu_read_unlock();
+
+ return ret;
+}
+
+static int netsec_xdp_setup(struct netsec_priv *priv, struct bpf_prog *prog)
+{
+ struct net_device *dev = priv->ndev;
+ struct bpf_prog *old_prog;
+
+ /* For now just support only the usual MTU sized frames */
+ if (prog && dev->mtu > 1500) {
+ netdev_warn(dev, "Jumbo frames not yet supported with XDP\n");
+ return -EOPNOTSUPP;
+ }
+
+ if (netif_running(dev))
+ netsec_netdev_stop(dev);
+
+ /* Detach old prog, if any */
+ old_prog = xchg(&priv->xdp_prog, prog);
+ if (old_prog)
+ bpf_prog_put(old_prog);
+
+ if (priv->xdp_prog) {
+ /* Attach BPF program */
+ priv->xdp_prog = bpf_prog_add(priv->xdp_prog, 1);
+ if (IS_ERR(priv->xdp_prog))
+ return PTR_ERR(priv->xdp_prog);
+ }
+
+ if (netif_running(dev))
+ netsec_netdev_open(dev);
+
+ return 0;
+}
+
+static int netsec_xdp(struct net_device *ndev, struct netdev_bpf *xdp)
+{
+ struct netsec_priv *priv = netdev_priv(ndev);
+
+ switch (xdp->command) {
+ case XDP_SETUP_PROG:
+ return netsec_xdp_setup(priv, xdp->prog);
+ case XDP_QUERY_PROG:
+ xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
+ return 0;
+ default:
+ return -EINVAL;
+ }
+}
+
static const struct net_device_ops netsec_netdev_ops = {
.ndo_init = netsec_netdev_init,
.ndo_uninit = netsec_netdev_uninit,
@@ -1430,6 +1622,7 @@ static const struct net_device_ops netsec_netdev_ops = {
.ndo_set_mac_address = eth_mac_addr,
.ndo_validate_addr = eth_validate_addr,
.ndo_do_ioctl = netsec_netdev_ioctl,
+ .ndo_bpf = netsec_xdp,
};
static int netsec_of_probe(struct platform_device *pdev,
--
2.7.4
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [net-next, PATCH 0/2, v1] net: socionext: add AF_XDP support
2018-09-10 8:24 [net-next, PATCH 0/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 1/2, v1] net: socionext: different approach on DMA Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
@ 2018-09-10 10:06 ` Björn Töpel
2 siblings, 0 replies; 8+ messages in thread
From: Björn Töpel @ 2018-09-10 10:06 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: Netdev, jaswinder.singh, ard.biesheuvel, masami.hiramatsu,
Arnd Bergmann, MykytaI Iziumtsev, Björn Töpel,
Karlsson, Magnus
Den mån 10 sep. 2018 kl 10:26 skrev Ilias Apalodimas
<ilias.apalodimas@linaro.org>:
>
> This patch series adds AF_XDP support socionext netsec driver
>
> - patch [1/2]: Use a different allocation scheme for Rx DMA buffers to prepare
> the driver for AF_XDP support
> - patch [2/2]: Add AF_XDP support without zero-copy
>
> Ilias Apalodimas (2):
> net: socionext: different approach on DMA
> net: socionext: add AF_XDP support
>
You should probably rephrase patch #2. You are adding XDP support, and
AF_XDP just follows from that. Nice to see more XDP support!
Björn
> drivers/net/ethernet/socionext/netsec.c | 444 +++++++++++++++++++++++---------
> 1 file changed, 329 insertions(+), 115 deletions(-)
>
> --
> 2.7.4
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support
2018-09-10 8:24 ` [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
@ 2018-09-10 10:56 ` Toshiaki Makita
2018-09-10 11:20 ` Ilias Apalodimas
2018-09-10 16:21 ` Ilias Apalodimas
0 siblings, 2 replies; 8+ messages in thread
From: Toshiaki Makita @ 2018-09-10 10:56 UTC (permalink / raw)
To: Ilias Apalodimas, netdev, jaswinder.singh
Cc: ard.biesheuvel, masami.hiramatsu, arnd, mykyta.iziumtsev,
bjorn.topel, magnus.karlsson
On 2018/09/10 17:24, Ilias Apalodimas wrote:
> Add basic AF_XDP support without zero-copy
>
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> ---
...
> @@ -707,6 +731,26 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
> if (unlikely(!buf_addr))
> break;
>
> + if (xdp_prog) {
> + xdp_result = netsec_run_xdp(desc, priv, xdp_prog,
> + pkt_len);
> + if (xdp_result != NETSEC_XDP_PASS) {
> + xdp_flush |= xdp_result & NETSEC_XDP_REDIR;
> +
> + dma_unmap_single_attrs(priv->dev,
> + desc->dma_addr,
> + desc->len, DMA_TO_DEVICE,
> + DMA_ATTR_SKIP_CPU_SYNC);
> +
> + desc->len = desc_len;
> + desc->dma_addr = dma_handle;
> + desc->addr = buf_addr;
> + netsec_rx_fill(priv, idx, 1);
> + nsetsec_adv_desc(&dring->tail);
> + }
> + continue;
Continue even on XDP_PASS? Is this really correct?
Also seems there is no handling of adjust_head/tail for XDP_PASS case.
> + }
> +
> skb = build_skb(desc->addr, desc->len);
> if (unlikely(!skb)) {
> dma_unmap_single(priv->dev, dma_handle, desc_len,
> @@ -740,6 +784,9 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
> nsetsec_adv_desc(&dring->tail);
> }
>
> + if (xdp_flush & NETSEC_XDP_REDIR)
> + xdp_do_flush_map();
> +
> return done;
> }
...
> +static u32 netsec_run_xdp(struct netsec_desc *desc, struct netsec_priv *priv,
> + struct bpf_prog *prog, u16 len)
> +
> +{
> + struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
> + struct xdp_buff xdp;
> + u32 ret = NETSEC_XDP_PASS;
> + int err;
> + u32 act;
> +
> + xdp.data_hard_start = desc->addr;
> + xdp.data = desc->addr;
There is no headroom. REDIRECT using devmap/cpumap will fail due to
this. Generally we need XDP_PACKET_HEADROOM.
> + xdp_set_data_meta_invalid(&xdp);
> + xdp.data_end = xdp.data + len;
> + xdp.rxq = &dring->xdp_rxq;
> +
> + rcu_read_lock();
> + act = bpf_prog_run_xdp(prog, &xdp);
> +
> + switch (act) {
> + case XDP_PASS:
> + ret = NETSEC_XDP_PASS;
> + break;
> + case XDP_TX:
> + ret = netsec_xmit_xdp(priv, &xdp, desc);
> + break;
> + case XDP_REDIRECT:
> + err = xdp_do_redirect(priv->ndev, &xdp, prog);
> + if (!err) {
> + ret = NETSEC_XDP_REDIR;
> + } else {
> + ret = NETSEC_XDP_CONSUMED;
> + xdp_return_buff(&xdp);
> + }
> + break;
> + default:
> + bpf_warn_invalid_xdp_action(act);
> + /* fall through */
> + case XDP_ABORTED:
> + trace_xdp_exception(priv->ndev, prog, act);
> + /* fall through -- handle aborts by dropping packet */
> + case XDP_DROP:
> + ret = NETSEC_XDP_CONSUMED;
> + break;
> + }
> +
> + rcu_read_unlock();
> +
> + return ret;
> +}
> +
> +static int netsec_xdp_setup(struct netsec_priv *priv, struct bpf_prog *prog)
> +{
> + struct net_device *dev = priv->ndev;
> + struct bpf_prog *old_prog;
> +
> + /* For now just support only the usual MTU sized frames */
> + if (prog && dev->mtu > 1500) {
> + netdev_warn(dev, "Jumbo frames not yet supported with XDP\n");
Why not using extack?
> + return -EOPNOTSUPP;
> + }
> +
--
Toshiaki Makita
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support
2018-09-10 10:56 ` Toshiaki Makita
@ 2018-09-10 11:20 ` Ilias Apalodimas
2018-09-10 16:21 ` Ilias Apalodimas
1 sibling, 0 replies; 8+ messages in thread
From: Ilias Apalodimas @ 2018-09-10 11:20 UTC (permalink / raw)
To: Toshiaki Makita
Cc: netdev, jaswinder.singh, ard.biesheuvel, masami.hiramatsu, arnd,
mykyta.iziumtsev, bjorn.topel, magnus.karlsson
On Mon, Sep 10, 2018 at 07:56:49PM +0900, Toshiaki Makita wrote:
> On 2018/09/10 17:24, Ilias Apalodimas wrote:
> > Add basic AF_XDP support without zero-copy
> >
> > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > ---
> ...
> > @@ -707,6 +731,26 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
> > if (unlikely(!buf_addr))
> > break;
> >
> > + if (xdp_prog) {
> > + xdp_result = netsec_run_xdp(desc, priv, xdp_prog,
> > + pkt_len);
> > + if (xdp_result != NETSEC_XDP_PASS) {
> > + xdp_flush |= xdp_result & NETSEC_XDP_REDIR;
> > +
> > + dma_unmap_single_attrs(priv->dev,
> > + desc->dma_addr,
> > + desc->len, DMA_TO_DEVICE,
> > + DMA_ATTR_SKIP_CPU_SYNC);
> > +
> > + desc->len = desc_len;
> > + desc->dma_addr = dma_handle;
> > + desc->addr = buf_addr;
> > + netsec_rx_fill(priv, idx, 1);
> > + nsetsec_adv_desc(&dring->tail);
> > + }
> > + continue;
>
> Continue even on XDP_PASS? Is this really correct?
>
> Also seems there is no handling of adjust_head/tail for XDP_PASS case.
No continue is wrong there, thanks for the catch. This should return the packet
to the network stack. I'll fix it on v2.
>
> > + }
> > +
> > skb = build_skb(desc->addr, desc->len);
> > if (unlikely(!skb)) {
> > dma_unmap_single(priv->dev, dma_handle, desc_len,
> > @@ -740,6 +784,9 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
> > nsetsec_adv_desc(&dring->tail);
> > }
> >
> > + if (xdp_flush & NETSEC_XDP_REDIR)
> > + xdp_do_flush_map();
> > +
> > return done;
> > }
> ...
> > +static u32 netsec_run_xdp(struct netsec_desc *desc, struct netsec_priv *priv,
> > + struct bpf_prog *prog, u16 len)
> > +
> > +{
> > + struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX];
> > + struct xdp_buff xdp;
> > + u32 ret = NETSEC_XDP_PASS;
> > + int err;
> > + u32 act;
> > +
> > + xdp.data_hard_start = desc->addr;
> > + xdp.data = desc->addr;
>
> There is no headroom. REDIRECT using devmap/cpumap will fail due to
> this. Generally we need XDP_PACKET_HEADROOM.
>
Ok, i'll modify the patch to include XDP_PACKET_HEADROOM
> > + xdp_set_data_meta_invalid(&xdp);
> > + xdp.data_end = xdp.data + len;
> > + xdp.rxq = &dring->xdp_rxq;
> > +
> > + rcu_read_lock();
> > + act = bpf_prog_run_xdp(prog, &xdp);
> > +
> > + switch (act) {
> > + case XDP_PASS:
> > + ret = NETSEC_XDP_PASS;
> > + break;
> > + case XDP_TX:
> > + ret = netsec_xmit_xdp(priv, &xdp, desc);
> > + break;
> > + case XDP_REDIRECT:
> > + err = xdp_do_redirect(priv->ndev, &xdp, prog);
> > + if (!err) {
> > + ret = NETSEC_XDP_REDIR;
> > + } else {
> > + ret = NETSEC_XDP_CONSUMED;
> > + xdp_return_buff(&xdp);
> > + }
> > + break;
> > + default:
> > + bpf_warn_invalid_xdp_action(act);
> > + /* fall through */
> > + case XDP_ABORTED:
> > + trace_xdp_exception(priv->ndev, prog, act);
> > + /* fall through -- handle aborts by dropping packet */
> > + case XDP_DROP:
> > + ret = NETSEC_XDP_CONSUMED;
> > + break;
> > + }
> > +
> > + rcu_read_unlock();
> > +
> > + return ret;
> > +}
> > +
> > +static int netsec_xdp_setup(struct netsec_priv *priv, struct bpf_prog *prog)
> > +{
> > + struct net_device *dev = priv->ndev;
> > + struct bpf_prog *old_prog;
> > +
> > + /* For now just support only the usual MTU sized frames */
> > + if (prog && dev->mtu > 1500) {
> > + netdev_warn(dev, "Jumbo frames not yet supported with XDP\n");
>
> Why not using extack?
>
Wasn't aware that this should use the extack for errors. I just saw the same
check on an existing driver and thought that was the way to go
> > + return -EOPNOTSUPP;
> > + }
> > +
Thanks for having a look!
/Ilias
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support
2018-09-10 10:56 ` Toshiaki Makita
2018-09-10 11:20 ` Ilias Apalodimas
@ 2018-09-10 16:21 ` Ilias Apalodimas
2018-09-11 0:45 ` Toshiaki Makita
1 sibling, 1 reply; 8+ messages in thread
From: Ilias Apalodimas @ 2018-09-10 16:21 UTC (permalink / raw)
To: Toshiaki Makita
Cc: netdev, jaswinder.singh, ard.biesheuvel, masami.hiramatsu, arnd,
mykyta.iziumtsev, bjorn.topel, magnus.karlsson
> > @@ -707,6 +731,26 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
> > if (unlikely(!buf_addr))
> > break;
> >
> > + if (xdp_prog) {
> > + xdp_result = netsec_run_xdp(desc, priv, xdp_prog,
> > + pkt_len);
> > + if (xdp_result != NETSEC_XDP_PASS) {
> > + xdp_flush |= xdp_result & NETSEC_XDP_REDIR;
> > +
> > + dma_unmap_single_attrs(priv->dev,
> > + desc->dma_addr,
> > + desc->len, DMA_TO_DEVICE,
> > + DMA_ATTR_SKIP_CPU_SYNC);
> > +
> > + desc->len = desc_len;
> > + desc->dma_addr = dma_handle;
> > + desc->addr = buf_addr;
> > + netsec_rx_fill(priv, idx, 1);
> > + nsetsec_adv_desc(&dring->tail);
> > + }
> > + continue;
>
> Continue even on XDP_PASS? Is this really correct?
>
> Also seems there is no handling of adjust_head/tail for XDP_PASS case.
>
A question on this. Should XDP related frames be allocated using 1 page
per packet?
Thanks
Ilias
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support
2018-09-10 16:21 ` Ilias Apalodimas
@ 2018-09-11 0:45 ` Toshiaki Makita
0 siblings, 0 replies; 8+ messages in thread
From: Toshiaki Makita @ 2018-09-11 0:45 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: netdev, jaswinder.singh, ard.biesheuvel, masami.hiramatsu, arnd,
mykyta.iziumtsev, bjorn.topel, magnus.karlsson,
Jesper Dangaard Brouer, Alexei Starovoitov, Daniel Borkmann,
Björn Töpel
On 2018/09/11 1:21, Ilias Apalodimas wrote:
>>> @@ -707,6 +731,26 @@ static int netsec_process_rx(struct netsec_priv *priv, int budget)
>>> if (unlikely(!buf_addr))
>>> break;
>>>
>>> + if (xdp_prog) {
>>> + xdp_result = netsec_run_xdp(desc, priv, xdp_prog,
>>> + pkt_len);
>>> + if (xdp_result != NETSEC_XDP_PASS) {
>>> + xdp_flush |= xdp_result & NETSEC_XDP_REDIR;
>>> +
>>> + dma_unmap_single_attrs(priv->dev,
>>> + desc->dma_addr,
>>> + desc->len, DMA_TO_DEVICE,
>>> + DMA_ATTR_SKIP_CPU_SYNC);
>>> +
>>> + desc->len = desc_len;
>>> + desc->dma_addr = dma_handle;
>>> + desc->addr = buf_addr;
>>> + netsec_rx_fill(priv, idx, 1);
>>> + nsetsec_adv_desc(&dring->tail);
>>> + }
>>> + continue;
>>
>> Continue even on XDP_PASS? Is this really correct?
>>
>> Also seems there is no handling of adjust_head/tail for XDP_PASS case.
>>
> A question on this. Should XDP related frames be allocated using 1 page
> per packet?
AFAIK there is no such constraint, e.g. i40e allocates 1 page per 2 packets.
--
Toshiaki Makita
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2018-09-11 5:45 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-10 8:24 [net-next, PATCH 0/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 1/2, v1] net: socionext: different approach on DMA Ilias Apalodimas
2018-09-10 8:24 ` [net-next, PATCH 2/2, v1] net: socionext: add AF_XDP support Ilias Apalodimas
2018-09-10 10:56 ` Toshiaki Makita
2018-09-10 11:20 ` Ilias Apalodimas
2018-09-10 16:21 ` Ilias Apalodimas
2018-09-11 0:45 ` Toshiaki Makita
2018-09-10 10:06 ` [net-next, PATCH 0/2, " Björn Töpel
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.