Skip to content

Commit 4e3c60e

Browse files
committed
Merge branch 'generic-xdp-followups'
Daniel Borkmann says: ==================== Two generic xdp related follow-ups Two follow-ups for the generic XDP API, would be great if both could still be considered, since the XDP API is not frozen yet. For details please see individual patches. v1 -> v2: - Implemented feedback from Jakub Kicinski (reusing attribute on dump), thanks! - Rest as is. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 0a5539f + d67b9cd commit 4e3c60e

6 files changed

Lines changed: 90 additions & 48 deletions

File tree

‎include/linux/netdevice.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,11 +3296,15 @@ int dev_get_phys_port_id(struct net_device *dev,
32963296
int dev_get_phys_port_name(struct net_device *dev,
32973297
char *name, size_t len);
32983298
int dev_change_proto_down(struct net_device *dev, bool proto_down);
3299-
int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
3300-
int fd, u32 flags);
33013299
struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev);
33023300
struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
33033301
struct netdev_queue *txq, int *ret);
3302+
3303+
typedef int (*xdp_op_t)(struct net_device *dev, struct netdev_xdp *xdp);
3304+
int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
3305+
int fd, u32 flags);
3306+
bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
3307+
33043308
int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
33053309
int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
33063310
bool is_skb_forwardable(const struct net_device *dev,

‎include/uapi/linux/if_link.h‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,18 @@ enum {
888888
/* XDP section */
889889

890890
#define XDP_FLAGS_UPDATE_IF_NOEXIST (1U << 0)
891-
#define XDP_FLAGS_SKB_MODE (2U << 0)
891+
#define XDP_FLAGS_SKB_MODE (1U << 1)
892+
#define XDP_FLAGS_DRV_MODE (1U << 2)
892893
#define XDP_FLAGS_MASK (XDP_FLAGS_UPDATE_IF_NOEXIST | \
893-
XDP_FLAGS_SKB_MODE)
894+
XDP_FLAGS_SKB_MODE | \
895+
XDP_FLAGS_DRV_MODE)
896+
897+
/* These are stored into IFLA_XDP_ATTACHED on dump. */
898+
enum {
899+
XDP_ATTACHED_NONE = 0,
900+
XDP_ATTACHED_DRV,
901+
XDP_ATTACHED_SKB,
902+
};
894903

895904
enum {
896905
IFLA_XDP_UNSPEC,

‎net/core/dev.c‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6852,6 +6852,32 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down)
68526852
}
68536853
EXPORT_SYMBOL(dev_change_proto_down);
68546854

6855+
bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
6856+
{
6857+
struct netdev_xdp xdp;
6858+
6859+
memset(&xdp, 0, sizeof(xdp));
6860+
xdp.command = XDP_QUERY_PROG;
6861+
6862+
/* Query must always succeed. */
6863+
WARN_ON(xdp_op(dev, &xdp) < 0);
6864+
return xdp.prog_attached;
6865+
}
6866+
6867+
static int dev_xdp_install(struct net_device *dev, xdp_op_t xdp_op,
6868+
struct netlink_ext_ack *extack,
6869+
struct bpf_prog *prog)
6870+
{
6871+
struct netdev_xdp xdp;
6872+
6873+
memset(&xdp, 0, sizeof(xdp));
6874+
xdp.command = XDP_SETUP_PROG;
6875+
xdp.extack = extack;
6876+
xdp.prog = prog;
6877+
6878+
return xdp_op(dev, &xdp);
6879+
}
6880+
68556881
/**
68566882
* dev_change_xdp_fd - set or clear a bpf program for a device rx path
68576883
* @dev: device
@@ -6864,41 +6890,34 @@ EXPORT_SYMBOL(dev_change_proto_down);
68646890
int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
68656891
int fd, u32 flags)
68666892
{
6867-
int (*xdp_op)(struct net_device *dev, struct netdev_xdp *xdp);
68686893
const struct net_device_ops *ops = dev->netdev_ops;
68696894
struct bpf_prog *prog = NULL;
6870-
struct netdev_xdp xdp;
6895+
xdp_op_t xdp_op, xdp_chk;
68716896
int err;
68726897

68736898
ASSERT_RTNL();
68746899

6875-
xdp_op = ops->ndo_xdp;
6900+
xdp_op = xdp_chk = ops->ndo_xdp;
6901+
if (!xdp_op && (flags & XDP_FLAGS_DRV_MODE))
6902+
return -EOPNOTSUPP;
68766903
if (!xdp_op || (flags & XDP_FLAGS_SKB_MODE))
68776904
xdp_op = generic_xdp_install;
6905+
if (xdp_op == xdp_chk)
6906+
xdp_chk = generic_xdp_install;
68786907

68796908
if (fd >= 0) {
6880-
if (flags & XDP_FLAGS_UPDATE_IF_NOEXIST) {
6881-
memset(&xdp, 0, sizeof(xdp));
6882-
xdp.command = XDP_QUERY_PROG;
6883-
6884-
err = xdp_op(dev, &xdp);
6885-
if (err < 0)
6886-
return err;
6887-
if (xdp.prog_attached)
6888-
return -EBUSY;
6889-
}
6909+
if (xdp_chk && __dev_xdp_attached(dev, xdp_chk))
6910+
return -EEXIST;
6911+
if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
6912+
__dev_xdp_attached(dev, xdp_op))
6913+
return -EBUSY;
68906914

68916915
prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
68926916
if (IS_ERR(prog))
68936917
return PTR_ERR(prog);
68946918
}
68956919

6896-
memset(&xdp, 0, sizeof(xdp));
6897-
xdp.command = XDP_SETUP_PROG;
6898-
xdp.extack = extack;
6899-
xdp.prog = prog;
6900-
6901-
err = xdp_op(dev, &xdp);
6920+
err = dev_xdp_install(dev, xdp_op, extack, prog);
69026921
if (err < 0 && prog)
69036922
bpf_prog_put(prog);
69046923

‎net/core/rtnetlink.c‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,7 @@ static size_t rtnl_port_size(const struct net_device *dev,
899899
static size_t rtnl_xdp_size(void)
900900
{
901901
size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */
902-
nla_total_size(1) + /* XDP_ATTACHED */
903-
nla_total_size(4); /* XDP_FLAGS */
902+
nla_total_size(1); /* XDP_ATTACHED */
904903

905904
return xdp_size;
906905
}
@@ -1247,37 +1246,34 @@ static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
12471246
return 0;
12481247
}
12491248

1249+
static u8 rtnl_xdp_attached_mode(struct net_device *dev)
1250+
{
1251+
const struct net_device_ops *ops = dev->netdev_ops;
1252+
1253+
ASSERT_RTNL();
1254+
1255+
if (rcu_access_pointer(dev->xdp_prog))
1256+
return XDP_ATTACHED_SKB;
1257+
if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp))
1258+
return XDP_ATTACHED_DRV;
1259+
1260+
return XDP_ATTACHED_NONE;
1261+
}
1262+
12501263
static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
12511264
{
12521265
struct nlattr *xdp;
1253-
u32 xdp_flags = 0;
1254-
u8 val = 0;
12551266
int err;
12561267

12571268
xdp = nla_nest_start(skb, IFLA_XDP);
12581269
if (!xdp)
12591270
return -EMSGSIZE;
1260-
if (rcu_access_pointer(dev->xdp_prog)) {
1261-
xdp_flags = XDP_FLAGS_SKB_MODE;
1262-
val = 1;
1263-
} else if (dev->netdev_ops->ndo_xdp) {
1264-
struct netdev_xdp xdp_op = {};
1265-
1266-
xdp_op.command = XDP_QUERY_PROG;
1267-
err = dev->netdev_ops->ndo_xdp(dev, &xdp_op);
1268-
if (err)
1269-
goto err_cancel;
1270-
val = xdp_op.prog_attached;
1271-
}
1272-
err = nla_put_u8(skb, IFLA_XDP_ATTACHED, val);
1271+
1272+
err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
1273+
rtnl_xdp_attached_mode(dev));
12731274
if (err)
12741275
goto err_cancel;
12751276

1276-
if (xdp_flags) {
1277-
err = nla_put_u32(skb, IFLA_XDP_FLAGS, xdp_flags);
1278-
if (err)
1279-
goto err_cancel;
1280-
}
12811277
nla_nest_end(skb, xdp);
12821278
return 0;
12831279

@@ -2199,6 +2195,11 @@ static int do_setlink(const struct sk_buff *skb,
21992195
err = -EINVAL;
22002196
goto errout;
22012197
}
2198+
if ((xdp_flags & XDP_FLAGS_SKB_MODE) &&
2199+
(xdp_flags & XDP_FLAGS_DRV_MODE)) {
2200+
err = -EINVAL;
2201+
goto errout;
2202+
}
22022203
}
22032204

22042205
if (xdp[IFLA_XDP_FD]) {

‎samples/bpf/xdp1_user.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ static void usage(const char *prog)
6262
fprintf(stderr,
6363
"usage: %s [OPTS] IFINDEX\n\n"
6464
"OPTS:\n"
65-
" -S use skb-mode\n",
65+
" -S use skb-mode\n"
66+
" -N enforce native mode\n",
6667
prog);
6768
}
6869

6970
int main(int argc, char **argv)
7071
{
71-
const char *optstr = "S";
72+
const char *optstr = "SN";
7273
char filename[256];
7374
int opt;
7475

@@ -77,6 +78,9 @@ int main(int argc, char **argv)
7778
case 'S':
7879
xdp_flags |= XDP_FLAGS_SKB_MODE;
7980
break;
81+
case 'N':
82+
xdp_flags |= XDP_FLAGS_DRV_MODE;
83+
break;
8084
default:
8185
usage(basename(argv[0]));
8286
return 1;

‎samples/bpf/xdp_tx_iptunnel_user.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ static void usage(const char *cmd)
7979
printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
8080
printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
8181
printf(" -P <IP-Protocol> Default is TCP\n");
82+
printf(" -S use skb-mode\n");
83+
printf(" -N enforce native mode\n");
8284
printf(" -h Display this help\n");
8385
}
8486

@@ -138,7 +140,7 @@ int main(int argc, char **argv)
138140
{
139141
unsigned char opt_flags[256] = {};
140142
unsigned int kill_after_s = 0;
141-
const char *optstr = "i:a:p:s:d:m:T:P:Sh";
143+
const char *optstr = "i:a:p:s:d:m:T:P:SNh";
142144
int min_port = 0, max_port = 0;
143145
struct iptnl_info tnl = {};
144146
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
@@ -206,6 +208,9 @@ int main(int argc, char **argv)
206208
case 'S':
207209
xdp_flags |= XDP_FLAGS_SKB_MODE;
208210
break;
211+
case 'N':
212+
xdp_flags |= XDP_FLAGS_DRV_MODE;
213+
break;
209214
default:
210215
usage(argv[0]);
211216
return 1;

0 commit comments

Comments
 (0)