]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink_ipoib.c
ip: allow to specify mode for sit tunnels
[mirror_iproute2.git] / ip / iplink_ipoib.c
1 /*
2 * iplink_ipoib.c IPoIB device support
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Or Gerlitz <ogerlitz@mellanox.com>
10 * copied iflink_vlan.c authored by Patrick McHardy <kaber@trash.net>
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <linux/if_link.h>
17
18 #include "rt_names.h"
19 #include "utils.h"
20 #include "ip_common.h"
21
22 static void explain(void)
23 {
24 fprintf(stderr,
25 "Usage: ... ipoib [pkey PKEY] [mode {datagram | connected}]"
26 "[umcast {0|1}]\n"
27 "\n"
28 "PKEY := 0x8001-0xffff\n"
29 );
30 }
31
32
33 static int mode_arg(void)
34 {
35 fprintf(stderr, "Error: argument of \"mode\" must be \"datagram\""
36 "or \"connected\"\n");
37 return -1;
38 }
39
40 static int ipoib_parse_opt(struct link_util *lu, int argc, char **argv,
41 struct nlmsghdr *n)
42 {
43 __u16 pkey, mode, umcast;
44
45 while (argc > 0) {
46 if (matches(*argv, "pkey") == 0) {
47 NEXT_ARG();
48 if (get_u16(&pkey, *argv, 0))
49 invarg("pkey is invalid", *argv);
50 addattr_l(n, 1024, IFLA_IPOIB_PKEY, &pkey, 2);
51 } else if (matches(*argv, "mode") == 0) {
52 NEXT_ARG();
53 if (strcmp(*argv, "datagram") == 0)
54 mode = IPOIB_MODE_DATAGRAM;
55 else if (strcmp(*argv, "connected") == 0)
56 mode = IPOIB_MODE_CONNECTED;
57 else
58 return mode_arg();
59 addattr_l(n, 1024, IFLA_IPOIB_MODE, &mode, 2);
60 } else if (matches(*argv, "umcast") == 0) {
61 NEXT_ARG();
62 if (get_u16(&umcast, *argv, 0))
63 invarg("umcast is invalid", *argv);
64 addattr_l(n, 1024, IFLA_IPOIB_UMCAST, &umcast, 2);
65 } else if (matches(*argv, "help") == 0) {
66 explain();
67 return -1;
68 } else {
69 fprintf(stderr, "ipoib: unknown option \"%s\"?\n", *argv);
70 explain();
71 return -1;
72 }
73 argc--, argv++;
74 }
75
76 return 0;
77 }
78
79 static void ipoib_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
80 {
81 __u16 mode;
82
83 if (!tb)
84 return;
85
86 if (!tb[IFLA_IPOIB_PKEY] ||
87 RTA_PAYLOAD(tb[IFLA_IPOIB_PKEY]) < sizeof(__u16))
88 return;
89
90 fprintf(f, "pkey %#.4x ", rta_getattr_u16(tb[IFLA_IPOIB_PKEY]));
91
92 if (!tb[IFLA_IPOIB_MODE] ||
93 RTA_PAYLOAD(tb[IFLA_IPOIB_MODE]) < sizeof(__u16))
94 return;
95
96 mode = rta_getattr_u16(tb[IFLA_IPOIB_MODE]);
97 fprintf(f, "mode %s ",
98 mode == IPOIB_MODE_DATAGRAM ? "datagram" :
99 mode == IPOIB_MODE_CONNECTED ? "connected" :
100 "unknown");
101
102 if (!tb[IFLA_IPOIB_UMCAST] ||
103 RTA_PAYLOAD(tb[IFLA_IPOIB_UMCAST]) < sizeof(__u16))
104 return;
105
106 fprintf(f, "umcast %.4x ", rta_getattr_u16(tb[IFLA_IPOIB_UMCAST]));
107 }
108
109 struct link_util ipoib_link_util = {
110 .id = "ipoib",
111 .maxattr = IFLA_IPOIB_MAX,
112 .parse_opt = ipoib_parse_opt,
113 .print_opt = ipoib_print_opt,
114 };