blob: cb204af4a25b579952e1652a860aff8d07a0ed39 [file] [log] [blame]
Or Gerlitzde038992012-10-25 16:57:59 +02001/*
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
vadimk561e6502014-09-30 08:17:31 +030022static void print_explain(FILE *f)
Or Gerlitzde038992012-10-25 16:57:59 +020023{
vadimk561e6502014-09-30 08:17:31 +030024 fprintf(f,
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070025 "Usage: ... ipoib [pkey PKEY] [mode {datagram | connected}][umcast {0|1}]\n"
Or Gerlitzde038992012-10-25 16:57:59 +020026 "\n"
27 "PKEY := 0x8001-0xffff\n"
28 );
29}
30
vadimk561e6502014-09-30 08:17:31 +030031static void explain(void)
32{
33 print_explain(stderr);
34}
Or Gerlitzde038992012-10-25 16:57:59 +020035
36static int mode_arg(void)
37{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070038 fprintf(stderr, "Error: argument of \"mode\" must be \"datagram\"or \"connected\"\n");
Or Gerlitzde038992012-10-25 16:57:59 +020039 return -1;
40}
41
42static int ipoib_parse_opt(struct link_util *lu, int argc, char **argv,
43 struct nlmsghdr *n)
44{
45 __u16 pkey, mode, umcast;
46
47 while (argc > 0) {
48 if (matches(*argv, "pkey") == 0) {
49 NEXT_ARG();
50 if (get_u16(&pkey, *argv, 0))
51 invarg("pkey is invalid", *argv);
52 addattr_l(n, 1024, IFLA_IPOIB_PKEY, &pkey, 2);
53 } else if (matches(*argv, "mode") == 0) {
54 NEXT_ARG();
55 if (strcmp(*argv, "datagram") == 0)
56 mode = IPOIB_MODE_DATAGRAM;
57 else if (strcmp(*argv, "connected") == 0)
58 mode = IPOIB_MODE_CONNECTED;
59 else
60 return mode_arg();
61 addattr_l(n, 1024, IFLA_IPOIB_MODE, &mode, 2);
62 } else if (matches(*argv, "umcast") == 0) {
63 NEXT_ARG();
64 if (get_u16(&umcast, *argv, 0))
65 invarg("umcast is invalid", *argv);
66 addattr_l(n, 1024, IFLA_IPOIB_UMCAST, &umcast, 2);
67 } else if (matches(*argv, "help") == 0) {
68 explain();
69 return -1;
70 } else {
Kees van Reeuwijk14645ec2013-02-08 03:32:36 +000071 fprintf(stderr, "ipoib: unknown option \"%s\"?\n", *argv);
Or Gerlitzde038992012-10-25 16:57:59 +020072 explain();
73 return -1;
74 }
75 argc--, argv++;
76 }
77
78 return 0;
79}
80
81static void ipoib_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
82{
83 __u16 mode;
84
85 if (!tb)
86 return;
87
88 if (!tb[IFLA_IPOIB_PKEY] ||
89 RTA_PAYLOAD(tb[IFLA_IPOIB_PKEY]) < sizeof(__u16))
90 return;
91
92 fprintf(f, "pkey %#.4x ", rta_getattr_u16(tb[IFLA_IPOIB_PKEY]));
93
94 if (!tb[IFLA_IPOIB_MODE] ||
95 RTA_PAYLOAD(tb[IFLA_IPOIB_MODE]) < sizeof(__u16))
96 return;
97
98 mode = rta_getattr_u16(tb[IFLA_IPOIB_MODE]);
99 fprintf(f, "mode %s ",
100 mode == IPOIB_MODE_DATAGRAM ? "datagram" :
101 mode == IPOIB_MODE_CONNECTED ? "connected" :
102 "unknown");
103
104 if (!tb[IFLA_IPOIB_UMCAST] ||
105 RTA_PAYLOAD(tb[IFLA_IPOIB_UMCAST]) < sizeof(__u16))
106 return;
107
108 fprintf(f, "umcast %.4x ", rta_getattr_u16(tb[IFLA_IPOIB_UMCAST]));
109}
110
vadimk561e6502014-09-30 08:17:31 +0300111static void ipoib_print_help(struct link_util *lu, int argc, char **argv,
112 FILE *f)
113{
114 print_explain(f);
115}
116
Or Gerlitzde038992012-10-25 16:57:59 +0200117struct link_util ipoib_link_util = {
118 .id = "ipoib",
119 .maxattr = IFLA_IPOIB_MAX,
120 .parse_opt = ipoib_parse_opt,
121 .print_opt = ipoib_print_opt,
vadimk561e6502014-09-30 08:17:31 +0300122 .print_help = ipoib_print_help,
Or Gerlitzde038992012-10-25 16:57:59 +0200123};