]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip: GTP support in ip link
authorWojciech Drewek <wojciech.drewek@intel.com>
Thu, 17 Mar 2022 16:27:54 +0000 (17:27 +0100)
committerDavid Ahern <dsahern@kernel.org>
Sun, 20 Mar 2022 23:40:22 +0000 (17:40 -0600)
Support for creating GTP devices through ip link. Two arguments
can be specified by the user when adding device of the GTP type.
 - role (sgsn or ggsn) - indicates whether we are on the GGSN or SGSN
 - hsize - indicates the size of the hash table where PDP sessions
   are stored

IFLA_GTP_FD0 and IFLA_GTP_FD1 arguments would not be provided. Those
are file descriptores to the sockets created in the userspace. Since
we are not going to create sockets in ip link, we don't have to
provide them.

Signed-off-by: Wojciech Drewek <wojciech.drewek@intel.com>
Reviewed-by: Harald Welte <laforge@gnumonks.org>
Signed-off-by: David Ahern <dsahern@kernel.org>
ip/Makefile
ip/iplink.c
ip/iplink_gtp.c [new file with mode: 0644]
man/man8/ip-link.8.in

index 11a361cef5de95d045b5d8b525918a9502548532..0f14c609a4f0e2e3dc93b9609f1f11d140667545 100644 (file)
@@ -12,7 +12,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \
     iplink_geneve.o iplink_vrf.o iproute_lwtunnel.o ipmacsec.o ipila.o \
     ipvrf.o iplink_xstats.o ipseg6.o iplink_netdevsim.o iplink_rmnet.o \
     ipnexthop.o ipmptcp.o iplink_bareudp.o iplink_wwan.o ipioam6.o \
-    iplink_amt.o iplink_batadv.o
+    iplink_amt.o iplink_batadv.o iplink_gtp.o
 
 RTMONOBJ=rtmon.o
 
index 1e8c51486f9b45316680df4f77cfa288ba50dc02..7accd3786586b7a3125007c0bf7cd579351086d0 100644 (file)
@@ -51,7 +51,7 @@ void iplink_types_usage(void)
        /* Remember to add new entry here if new type is added. */
        fprintf(stderr,
                "TYPE := { amt | bareudp | bond | bond_slave | bridge | bridge_slave |\n"
-               "          dummy | erspan | geneve | gre | gretap | ifb |\n"
+               "          dummy | erspan | geneve | gre | gretap | gtp | ifb |\n"
                "          ip6erspan | ip6gre | ip6gretap | ip6tnl |\n"
                "          ipip | ipoib | ipvlan | ipvtap |\n"
                "          macsec | macvlan | macvtap |\n"
diff --git a/ip/iplink_gtp.c b/ip/iplink_gtp.c
new file mode 100644 (file)
index 0000000..086946b
--- /dev/null
@@ -0,0 +1,140 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <stdio.h>
+
+#include "rt_names.h"
+#include "utils.h"
+#include "ip_common.h"
+
+#define GTP_ATTRSET(attrs, type) (((attrs) & (1L << (type))) != 0)
+
+static void print_explain(FILE *f)
+{
+       fprintf(f,
+               "Usage: ... gtp role ROLE\n"
+               "               [ hsize HSIZE ]\n"
+               "               [ restart_count RESTART_COUNT ]\n"
+               "\n"
+               "Where: ROLE            := { sgsn | ggsn }\n"
+               "       HSIZE           := 1-131071\n"
+               "       RESTART_COUNT   := 0-255\n"
+       );
+}
+
+static void check_duparg(__u32 *attrs, int type, const char *key,
+                        const char *argv)
+{
+       if (!GTP_ATTRSET(*attrs, type)) {
+               *attrs |= (1L << type);
+               return;
+       }
+       duparg2(key, argv);
+}
+
+static int gtp_parse_opt(struct link_util *lu, int argc, char **argv,
+                        struct nlmsghdr *n)
+{
+       __u32 attrs = 0;
+
+       /* When creating GTP device through ip link,
+        * this flag has to be set.
+        */
+       addattr8(n, 1024, IFLA_GTP_CREATE_SOCKETS, true);
+
+       while (argc > 0) {
+               if (!strcmp(*argv, "role")) {
+                       NEXT_ARG();
+                       check_duparg(&attrs, IFLA_GTP_ROLE, "role", *argv);
+                       if (!strcmp(*argv, "sgsn"))
+                               addattr32(n, 1024, IFLA_GTP_ROLE, GTP_ROLE_SGSN);
+                       else if (!strcmp(*argv, "ggsn"))
+                               addattr32(n, 1024, IFLA_GTP_ROLE, GTP_ROLE_GGSN);
+                       else
+                               invarg("invalid role, use sgsn or ggsn", *argv);
+               } else if (!strcmp(*argv, "hsize")) {
+                       __u32 hsize;
+
+                       NEXT_ARG();
+                       check_duparg(&attrs, IFLA_GTP_PDP_HASHSIZE, "hsize", *argv);
+
+                       if (get_u32(&hsize, *argv, 0))
+                               invarg("invalid PDP hash size", *argv);
+                       if (hsize >= 1u << 17)
+                               invarg("PDP hash size too big", *argv);
+                       addattr32(n, 1024, IFLA_GTP_PDP_HASHSIZE, hsize);
+               } else if (!strcmp(*argv, "restart_count")) {
+                       __u8 restart_count;
+
+                       NEXT_ARG();
+                       check_duparg(&attrs, IFLA_GTP_RESTART_COUNT, "restart_count", *argv);
+
+                       if (get_u8(&restart_count, *argv, 10))
+                               invarg("invalid restart_count", *argv);
+                       addattr8(n, 1024, IFLA_GTP_RESTART_COUNT, restart_count);
+               } else if (!strcmp(*argv, "help")) {
+                       print_explain(stderr);
+                       return -1;
+               }
+               argc--, argv++;
+       }
+
+       if (!GTP_ATTRSET(attrs, IFLA_GTP_ROLE)) {
+               fprintf(stderr, "gtp: role of the gtp device was not specified\n");
+               return -1;
+       }
+
+       if (!GTP_ATTRSET(attrs, IFLA_GTP_PDP_HASHSIZE))
+               addattr32(n, 1024, IFLA_GTP_PDP_HASHSIZE, 1024);
+
+       return 0;
+}
+
+static const char *gtp_role_to_string(__u32 role)
+{
+       switch (role) {
+       case GTP_ROLE_GGSN:
+               return "ggsn";
+       case GTP_ROLE_SGSN:
+               return "sgsn";
+       default:
+               return "unknown";
+       }
+}
+
+static void gtp_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
+{
+
+       if (tb[IFLA_GTP_ROLE]) {
+               __u32 role = rta_getattr_u32(tb[IFLA_GTP_ROLE]);
+
+               print_string(PRINT_ANY, "role", "role %s ",
+                            gtp_role_to_string(role));
+       }
+
+       if (tb[IFLA_GTP_PDP_HASHSIZE]) {
+               __u32 hsize = rta_getattr_u32(tb[IFLA_GTP_PDP_HASHSIZE]);
+
+               print_uint(PRINT_ANY, "hsize", "hsize %u ", hsize);
+       }
+
+       if (tb[IFLA_GTP_RESTART_COUNT]) {
+               __u8 restart_count = rta_getattr_u8(tb[IFLA_GTP_RESTART_COUNT]);
+
+               print_uint(PRINT_ANY, "restart_count",
+                          "restart_count %u ", restart_count);
+       }
+}
+
+static void gtp_print_help(struct link_util *lu, int argc, char **argv,
+                          FILE *f)
+{
+       print_explain(f);
+}
+
+struct link_util gtp_link_util = {
+       .id             = "gtp",
+       .maxattr        = IFLA_GTP_MAX,
+       .parse_opt      = gtp_parse_opt,
+       .print_opt      = gtp_print_opt,
+       .print_help     = gtp_print_help,
+};
index 93106d7f79ce0980ecdc0fd677c7b7c8fab5bbb8..7a675374746836093559816ce2de5e25abd6afb5 100644 (file)
@@ -243,7 +243,8 @@ ip-link \- network device configuration
 .BR macsec " |"
 .BR netdevsim " |"
 .BR rmnet " |"
-.BR xfrm " ]"
+.BR xfrm " |"
+.BR gtp " ]"
 
 .ti -8
 .IR ETYPE " := [ " TYPE " |"
@@ -392,6 +393,9 @@ Link types:
 .sp
 .BR xfrm
 - Virtual xfrm interface
+.sp
+.BR gtp
+- GPRS Tunneling Protocol
 .in -8
 
 .TP
@@ -1941,6 +1945,29 @@ policies. Policies must be configured with the same key. If not set, the key def
 
 .in -8
 
+.TP
+GTP Type Support
+For a link of type
+.I GTP
+the following additional arguments are supported:
+
+.BI "ip link add " DEVICE " type gtp role " ROLE " hsize " HSIZE
+
+.in +8
+.sp
+.BI role " ROLE "
+- specifies the role of the GTP device, either sgsn or ggsn
+
+.sp
+.BI hsize " HSIZE "
+- specifies size of the hashtable which stores PDP contexts
+
+.sp
+.BI restart_count " RESTART_COUNT "
+- GTP instance restart counter
+
+.in -8
+
 .SS ip link delete - delete virtual link
 
 .TP