bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [bpf-next v5] samples: bpf: add max_pckt_size option at xdp_adjust_tail
@ 2019-10-06 11:58 Daniel T. Lee
  2019-10-07  3:37 ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel T. Lee @ 2019-10-06 11:58 UTC (permalink / raw)
  To: Yonghong Song, Song Liu, Andrii Nakryiko, Daniel Borkmann,
	Alexei Starovoitov
  Cc: bpf, netdev

Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
to 600. To make this size flexible, static global variable
'max_pcktsz' is added.

By updating new packet size from the user space, xdp_adjust_tail_kern.o
will use this value as a new max packet size.

This static global variable can be accesible from .data section with
bpf_object__find_map* from user space, since it is considered as
internal map (accessible with .bss/.data/.rodata suffix).

If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
will be 600 as a default.

Changed the way to test prog_fd, map_fd from '!= 0' to '< 0',
since fd could be 0 when stdin is closed.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>

---
Changes in v5:
    - Change pcktsz map to static global variable
Changes in v4:
    - make pckt_size no less than ICMP_TOOBIG_SIZE
    - Fix code style
Changes in v2:
    - Change the helper to fetch map from 'bpf_map__next' to
    'bpf_object__find_map_fd_by_name'.

 samples/bpf/xdp_adjust_tail_kern.c |  7 +++++--
 samples/bpf/xdp_adjust_tail_user.c | 32 ++++++++++++++++++++++--------
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/samples/bpf/xdp_adjust_tail_kern.c b/samples/bpf/xdp_adjust_tail_kern.c
index 411fdb21f8bc..c616508befb9 100644
--- a/samples/bpf/xdp_adjust_tail_kern.c
+++ b/samples/bpf/xdp_adjust_tail_kern.c
@@ -25,6 +25,9 @@
 #define ICMP_TOOBIG_SIZE 98
 #define ICMP_TOOBIG_PAYLOAD_SIZE 92
 
+/* volatile to prevent compiler optimizations */
+static volatile __u32 max_pcktsz = MAX_PCKT_SIZE;
+
 struct bpf_map_def SEC("maps") icmpcnt = {
 	.type = BPF_MAP_TYPE_ARRAY,
 	.key_size = sizeof(__u32),
@@ -92,7 +95,7 @@ static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
 	orig_iph = data + off;
 	icmp_hdr->type = ICMP_DEST_UNREACH;
 	icmp_hdr->code = ICMP_FRAG_NEEDED;
-	icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr));
+	icmp_hdr->un.frag.mtu = htons(max_pcktsz - sizeof(struct ethhdr));
 	icmp_hdr->checksum = 0;
 	ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
 	icmp_hdr->checksum = csum;
@@ -121,7 +124,7 @@ static __always_inline int handle_ipv4(struct xdp_md *xdp)
 	int pckt_size = data_end - data;
 	int offset;
 
-	if (pckt_size > MAX_PCKT_SIZE) {
+	if (pckt_size > max(max_pcktsz, ICMP_TOOBIG_SIZE)) {
 		offset = pckt_size - ICMP_TOOBIG_SIZE;
 		if (bpf_xdp_adjust_tail(xdp, 0 - offset))
 			return XDP_PASS;
diff --git a/samples/bpf/xdp_adjust_tail_user.c b/samples/bpf/xdp_adjust_tail_user.c
index a3596b617c4c..bcdebd3be83e 100644
--- a/samples/bpf/xdp_adjust_tail_user.c
+++ b/samples/bpf/xdp_adjust_tail_user.c
@@ -23,6 +23,7 @@
 #include "libbpf.h"
 
 #define STATS_INTERVAL_S 2U
+#define MAX_PCKT_SIZE 600
 
 static int ifindex = -1;
 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
@@ -72,6 +73,7 @@ static void usage(const char *cmd)
 	printf("Usage: %s [...]\n", cmd);
 	printf("    -i <ifname|ifindex> Interface\n");
 	printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
+	printf("    -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
 	printf("    -S use skb-mode\n");
 	printf("    -N enforce native mode\n");
 	printf("    -F force loading prog\n");
@@ -85,13 +87,14 @@ int main(int argc, char **argv)
 		.prog_type	= BPF_PROG_TYPE_XDP,
 	};
 	unsigned char opt_flags[256] = {};
-	const char *optstr = "i:T:SNFh";
+	const char *optstr = "i:T:P:SNFh";
 	struct bpf_prog_info info = {};
 	__u32 info_len = sizeof(info);
 	unsigned int kill_after_s = 0;
 	int i, prog_fd, map_fd, opt;
 	struct bpf_object *obj;
-	struct bpf_map *map;
+	__u32 max_pckt_size = 0;
+	__u32 key = 0;
 	char filename[256];
 	int err;
 
@@ -110,6 +113,9 @@ int main(int argc, char **argv)
 		case 'T':
 			kill_after_s = atoi(optarg);
 			break;
+		case 'P':
+			max_pckt_size = atoi(optarg);
+			break;
 		case 'S':
 			xdp_flags |= XDP_FLAGS_SKB_MODE;
 			break;
@@ -150,15 +156,25 @@ int main(int argc, char **argv)
 	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
 		return 1;
 
-	map = bpf_map__next(NULL, obj);
-	if (!map) {
-		printf("finding a map in obj file failed\n");
+	if (prog_fd < 0) {
+		printf("load_bpf_file: %s\n", strerror(errno));
 		return 1;
 	}
-	map_fd = bpf_map__fd(map);
 
-	if (!prog_fd) {
-		printf("load_bpf_file: %s\n", strerror(errno));
+	/* static global var 'max_pcktsz' is accessible from .data section */
+	if (max_pckt_size) {
+		map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
+		if (map_fd < 0) {
+			printf("finding a max_pcktsz map in obj file failed\n");
+			return 1;
+		}
+		bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
+	}
+
+	/* fetch icmpcnt map */
+	map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
+	if (map_fd < 0) {
+		printf("finding a icmpcnt map in obj file failed\n");
 		return 1;
 	}
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [bpf-next v5] samples: bpf: add max_pckt_size option at xdp_adjust_tail
  2019-10-06 11:58 [bpf-next v5] samples: bpf: add max_pckt_size option at xdp_adjust_tail Daniel T. Lee
@ 2019-10-07  3:37 ` Andrii Nakryiko
  2019-10-07 11:25   ` Daniel T. Lee
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2019-10-07  3:37 UTC (permalink / raw)
  To: Daniel T. Lee
  Cc: Yonghong Song, Song Liu, Daniel Borkmann, Alexei Starovoitov,
	bpf, Networking

On Sun, Oct 6, 2019 at 4:58 AM Daniel T. Lee <danieltimlee@gmail.com> wrote:
>
> Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
> to 600. To make this size flexible, static global variable
> 'max_pcktsz' is added.
>
> By updating new packet size from the user space, xdp_adjust_tail_kern.o
> will use this value as a new max packet size.
>
> This static global variable can be accesible from .data section with
> bpf_object__find_map* from user space, since it is considered as
> internal map (accessible with .bss/.data/.rodata suffix).
>
> If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
> will be 600 as a default.
>
> Changed the way to test prog_fd, map_fd from '!= 0' to '< 0',
> since fd could be 0 when stdin is closed.
>
> Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
>
> ---

See nit below, but otherwise looks good.

Acked-by: Andrii Nakryiko <andriin@fb.com>

> Changes in v5:
>     - Change pcktsz map to static global variable
> Changes in v4:
>     - make pckt_size no less than ICMP_TOOBIG_SIZE
>     - Fix code style
> Changes in v2:
>     - Change the helper to fetch map from 'bpf_map__next' to
>     'bpf_object__find_map_fd_by_name'.
>
>  samples/bpf/xdp_adjust_tail_kern.c |  7 +++++--
>  samples/bpf/xdp_adjust_tail_user.c | 32 ++++++++++++++++++++++--------
>  2 files changed, 29 insertions(+), 10 deletions(-)
>
> diff --git a/samples/bpf/xdp_adjust_tail_kern.c b/samples/bpf/xdp_adjust_tail_kern.c
> index 411fdb21f8bc..c616508befb9 100644
> --- a/samples/bpf/xdp_adjust_tail_kern.c
> +++ b/samples/bpf/xdp_adjust_tail_kern.c
> @@ -25,6 +25,9 @@
>  #define ICMP_TOOBIG_SIZE 98
>  #define ICMP_TOOBIG_PAYLOAD_SIZE 92
>
> +/* volatile to prevent compiler optimizations */
> +static volatile __u32 max_pcktsz = MAX_PCKT_SIZE;
> +
>  struct bpf_map_def SEC("maps") icmpcnt = {
>         .type = BPF_MAP_TYPE_ARRAY,
>         .key_size = sizeof(__u32),
> @@ -92,7 +95,7 @@ static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
>         orig_iph = data + off;
>         icmp_hdr->type = ICMP_DEST_UNREACH;
>         icmp_hdr->code = ICMP_FRAG_NEEDED;
> -       icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr));
> +       icmp_hdr->un.frag.mtu = htons(max_pcktsz - sizeof(struct ethhdr));
>         icmp_hdr->checksum = 0;
>         ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
>         icmp_hdr->checksum = csum;
> @@ -121,7 +124,7 @@ static __always_inline int handle_ipv4(struct xdp_md *xdp)
>         int pckt_size = data_end - data;
>         int offset;
>
> -       if (pckt_size > MAX_PCKT_SIZE) {
> +       if (pckt_size > max(max_pcktsz, ICMP_TOOBIG_SIZE)) {
>                 offset = pckt_size - ICMP_TOOBIG_SIZE;
>                 if (bpf_xdp_adjust_tail(xdp, 0 - offset))
>                         return XDP_PASS;
> diff --git a/samples/bpf/xdp_adjust_tail_user.c b/samples/bpf/xdp_adjust_tail_user.c
> index a3596b617c4c..bcdebd3be83e 100644
> --- a/samples/bpf/xdp_adjust_tail_user.c
> +++ b/samples/bpf/xdp_adjust_tail_user.c
> @@ -23,6 +23,7 @@
>  #include "libbpf.h"
>
>  #define STATS_INTERVAL_S 2U
> +#define MAX_PCKT_SIZE 600
>
>  static int ifindex = -1;
>  static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
> @@ -72,6 +73,7 @@ static void usage(const char *cmd)
>         printf("Usage: %s [...]\n", cmd);
>         printf("    -i <ifname|ifindex> Interface\n");
>         printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
> +       printf("    -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
>         printf("    -S use skb-mode\n");
>         printf("    -N enforce native mode\n");
>         printf("    -F force loading prog\n");
> @@ -85,13 +87,14 @@ int main(int argc, char **argv)
>                 .prog_type      = BPF_PROG_TYPE_XDP,
>         };
>         unsigned char opt_flags[256] = {};
> -       const char *optstr = "i:T:SNFh";
> +       const char *optstr = "i:T:P:SNFh";
>         struct bpf_prog_info info = {};
>         __u32 info_len = sizeof(info);
>         unsigned int kill_after_s = 0;
>         int i, prog_fd, map_fd, opt;
>         struct bpf_object *obj;
> -       struct bpf_map *map;
> +       __u32 max_pckt_size = 0;
> +       __u32 key = 0;
>         char filename[256];
>         int err;
>
> @@ -110,6 +113,9 @@ int main(int argc, char **argv)
>                 case 'T':
>                         kill_after_s = atoi(optarg);
>                         break;
> +               case 'P':
> +                       max_pckt_size = atoi(optarg);
> +                       break;
>                 case 'S':
>                         xdp_flags |= XDP_FLAGS_SKB_MODE;
>                         break;
> @@ -150,15 +156,25 @@ int main(int argc, char **argv)
>         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
>                 return 1;
>
> -       map = bpf_map__next(NULL, obj);
> -       if (!map) {
> -               printf("finding a map in obj file failed\n");
> +       if (prog_fd < 0) {
> +               printf("load_bpf_file: %s\n", strerror(errno));

program load error is checked above (bpf_prog_load_xattr check), not
clear why check this again... Maybe just print helpful message there?

>                 return 1;
>         }
> -       map_fd = bpf_map__fd(map);
>
> -       if (!prog_fd) {
> -               printf("load_bpf_file: %s\n", strerror(errno));
> +       /* static global var 'max_pcktsz' is accessible from .data section */
> +       if (max_pckt_size) {
> +               map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
> +               if (map_fd < 0) {
> +                       printf("finding a max_pcktsz map in obj file failed\n");
> +                       return 1;
> +               }
> +               bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
> +       }
> +
> +       /* fetch icmpcnt map */
> +       map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
> +       if (map_fd < 0) {
> +               printf("finding a icmpcnt map in obj file failed\n");
>                 return 1;
>         }
>
> --
> 2.20.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [bpf-next v5] samples: bpf: add max_pckt_size option at xdp_adjust_tail
  2019-10-07  3:37 ` Andrii Nakryiko
@ 2019-10-07 11:25   ` Daniel T. Lee
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel T. Lee @ 2019-10-07 11:25 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Yonghong Song, Song Liu, Daniel Borkmann, Alexei Starovoitov,
	bpf, Networking

On Mon, Oct 7, 2019 at 12:37 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Oct 6, 2019 at 4:58 AM Daniel T. Lee <danieltimlee@gmail.com> wrote:
> >
> > Currently, at xdp_adjust_tail_kern.c, MAX_PCKT_SIZE is limited
> > to 600. To make this size flexible, static global variable
> > 'max_pcktsz' is added.
> >
> > By updating new packet size from the user space, xdp_adjust_tail_kern.o
> > will use this value as a new max packet size.
> >
> > This static global variable can be accesible from .data section with
> > bpf_object__find_map* from user space, since it is considered as
> > internal map (accessible with .bss/.data/.rodata suffix).
> >
> > If no '-P <MAX_PCKT_SIZE>' option is used, the size of maximum packet
> > will be 600 as a default.
> >
> > Changed the way to test prog_fd, map_fd from '!= 0' to '< 0',
> > since fd could be 0 when stdin is closed.
> >
> > Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
> >
> > ---
>
> See nit below, but otherwise looks good.
>
> Acked-by: Andrii Nakryiko <andriin@fb.com>
>
> > Changes in v5:
> >     - Change pcktsz map to static global variable
> > Changes in v4:
> >     - make pckt_size no less than ICMP_TOOBIG_SIZE
> >     - Fix code style
> > Changes in v2:
> >     - Change the helper to fetch map from 'bpf_map__next' to
> >     'bpf_object__find_map_fd_by_name'.
> >
> >  samples/bpf/xdp_adjust_tail_kern.c |  7 +++++--
> >  samples/bpf/xdp_adjust_tail_user.c | 32 ++++++++++++++++++++++--------
> >  2 files changed, 29 insertions(+), 10 deletions(-)
> >
> > diff --git a/samples/bpf/xdp_adjust_tail_kern.c b/samples/bpf/xdp_adjust_tail_kern.c
> > index 411fdb21f8bc..c616508befb9 100644
> > --- a/samples/bpf/xdp_adjust_tail_kern.c
> > +++ b/samples/bpf/xdp_adjust_tail_kern.c
> > @@ -25,6 +25,9 @@
> >  #define ICMP_TOOBIG_SIZE 98
> >  #define ICMP_TOOBIG_PAYLOAD_SIZE 92
> >
> > +/* volatile to prevent compiler optimizations */
> > +static volatile __u32 max_pcktsz = MAX_PCKT_SIZE;
> > +
> >  struct bpf_map_def SEC("maps") icmpcnt = {
> >         .type = BPF_MAP_TYPE_ARRAY,
> >         .key_size = sizeof(__u32),
> > @@ -92,7 +95,7 @@ static __always_inline int send_icmp4_too_big(struct xdp_md *xdp)
> >         orig_iph = data + off;
> >         icmp_hdr->type = ICMP_DEST_UNREACH;
> >         icmp_hdr->code = ICMP_FRAG_NEEDED;
> > -       icmp_hdr->un.frag.mtu = htons(MAX_PCKT_SIZE-sizeof(struct ethhdr));
> > +       icmp_hdr->un.frag.mtu = htons(max_pcktsz - sizeof(struct ethhdr));
> >         icmp_hdr->checksum = 0;
> >         ipv4_csum(icmp_hdr, ICMP_TOOBIG_PAYLOAD_SIZE, &csum);
> >         icmp_hdr->checksum = csum;
> > @@ -121,7 +124,7 @@ static __always_inline int handle_ipv4(struct xdp_md *xdp)
> >         int pckt_size = data_end - data;
> >         int offset;
> >
> > -       if (pckt_size > MAX_PCKT_SIZE) {
> > +       if (pckt_size > max(max_pcktsz, ICMP_TOOBIG_SIZE)) {
> >                 offset = pckt_size - ICMP_TOOBIG_SIZE;
> >                 if (bpf_xdp_adjust_tail(xdp, 0 - offset))
> >                         return XDP_PASS;
> > diff --git a/samples/bpf/xdp_adjust_tail_user.c b/samples/bpf/xdp_adjust_tail_user.c
> > index a3596b617c4c..bcdebd3be83e 100644
> > --- a/samples/bpf/xdp_adjust_tail_user.c
> > +++ b/samples/bpf/xdp_adjust_tail_user.c
> > @@ -23,6 +23,7 @@
> >  #include "libbpf.h"
> >
> >  #define STATS_INTERVAL_S 2U
> > +#define MAX_PCKT_SIZE 600
> >
> >  static int ifindex = -1;
> >  static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
> > @@ -72,6 +73,7 @@ static void usage(const char *cmd)
> >         printf("Usage: %s [...]\n", cmd);
> >         printf("    -i <ifname|ifindex> Interface\n");
> >         printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
> > +       printf("    -P <MAX_PCKT_SIZE> Default: %u\n", MAX_PCKT_SIZE);
> >         printf("    -S use skb-mode\n");
> >         printf("    -N enforce native mode\n");
> >         printf("    -F force loading prog\n");
> > @@ -85,13 +87,14 @@ int main(int argc, char **argv)
> >                 .prog_type      = BPF_PROG_TYPE_XDP,
> >         };
> >         unsigned char opt_flags[256] = {};
> > -       const char *optstr = "i:T:SNFh";
> > +       const char *optstr = "i:T:P:SNFh";
> >         struct bpf_prog_info info = {};
> >         __u32 info_len = sizeof(info);
> >         unsigned int kill_after_s = 0;
> >         int i, prog_fd, map_fd, opt;
> >         struct bpf_object *obj;
> > -       struct bpf_map *map;
> > +       __u32 max_pckt_size = 0;
> > +       __u32 key = 0;
> >         char filename[256];
> >         int err;
> >
> > @@ -110,6 +113,9 @@ int main(int argc, char **argv)
> >                 case 'T':
> >                         kill_after_s = atoi(optarg);
> >                         break;
> > +               case 'P':
> > +                       max_pckt_size = atoi(optarg);
> > +                       break;
> >                 case 'S':
> >                         xdp_flags |= XDP_FLAGS_SKB_MODE;
> >                         break;
> > @@ -150,15 +156,25 @@ int main(int argc, char **argv)
> >         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
> >                 return 1;
> >
> > -       map = bpf_map__next(NULL, obj);
> > -       if (!map) {
> > -               printf("finding a map in obj file failed\n");
> > +       if (prog_fd < 0) {
> > +               printf("load_bpf_file: %s\n", strerror(errno));
>
> program load error is checked above (bpf_prog_load_xattr check), not
> clear why check this again... Maybe just print helpful message there?
>

Oh, It won't be necessary since it gets error from 'bpf_prog_load_xattr'.
I'll resend the patch with this nit fixed.

Thanks for the review!

Thanks,
Daniel

> >                 return 1;
> >         }
> > -       map_fd = bpf_map__fd(map);
> >
> > -       if (!prog_fd) {
> > -               printf("load_bpf_file: %s\n", strerror(errno));
> > +       /* static global var 'max_pcktsz' is accessible from .data section */
> > +       if (max_pckt_size) {
> > +               map_fd = bpf_object__find_map_fd_by_name(obj, "xdp_adju.data");
> > +               if (map_fd < 0) {
> > +                       printf("finding a max_pcktsz map in obj file failed\n");
> > +                       return 1;
> > +               }
> > +               bpf_map_update_elem(map_fd, &key, &max_pckt_size, BPF_ANY);
> > +       }
> > +
> > +       /* fetch icmpcnt map */
> > +       map_fd = bpf_object__find_map_fd_by_name(obj, "icmpcnt");
> > +       if (map_fd < 0) {
> > +               printf("finding a icmpcnt map in obj file failed\n");
> >                 return 1;
> >         }
> >
> > --
> > 2.20.1
> >

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-10-07 11:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-06 11:58 [bpf-next v5] samples: bpf: add max_pckt_size option at xdp_adjust_tail Daniel T. Lee
2019-10-07  3:37 ` Andrii Nakryiko
2019-10-07 11:25   ` Daniel T. Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).