1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2011 Thomas Graf <[email protected]>
4 */
5
6 #include "nl-default.h"
7
8 #include <linux/netlink.h>
9
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/tc.h>
12 #include <netlink/cli/cls.h>
13 #include <netlink/cli/link.h>
14
15 #include "nl-priv-dynamic-route/nl-priv-dynamic-route.h"
16
17 static int quiet = 0;
18
print_usage(void)19 static void print_usage(void)
20 {
21 printf(
22 "Usage: nl-cls-add [OPTIONS]... classifier [CONFIGURATION]...\n"
23 "\n"
24 "OPTIONS\n"
25 " -q, --quiet Do not print informal notifications.\n"
26 " -h, --help Show this help text.\n"
27 " -v, --version Show versioning information.\n"
28 " --update Update classifier if it exists.\n"
29 " --update-only Only update classifier, never create it.\n"
30 " -d, --dev=DEV Network device the classifier should be attached to.\n"
31 " -i, --id=ID ID of new classifier (default: auto-generated)\n"
32 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
33 " --proto=PROTO Protocol to match (default: all)\n"
34 " --prio=PRIO Priority (default: 0)\n"
35 " --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
36 " --mpu=SIZE Minimum packet size on the link (default: 0).\n"
37 " --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
38 " --linktype=TYPE Overwrite linktype (default: type of network device)\n"
39 "\n"
40 "CONFIGURATION\n"
41 " -h, --help Show help text of classifier specific options.\n"
42 "\n"
43 "EXAMPLE\n"
44 " $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
45 "\n"
46 );
47 exit(0);
48 }
49
main(int argc,char * argv[])50 int main(int argc, char *argv[])
51 {
52 struct nl_sock *sock;
53 struct rtnl_cls *cls;
54 struct rtnl_tc *tc;
55 struct nl_cache *link_cache;
56 struct nl_dump_params dp = {
57 .dp_type = NL_DUMP_DETAILS,
58 .dp_fd = stdout,
59 };
60 struct nl_cli_tc_module *tm;
61 struct rtnl_tc_ops *ops;
62 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
63 char *kind, *id = NULL;
64
65 sock = nl_cli_alloc_socket();
66 nl_cli_connect(sock, NETLINK_ROUTE);
67
68 link_cache = nl_cli_link_alloc_cache(sock);
69
70 cls = nl_cli_cls_alloc();
71 tc = (struct rtnl_tc *) cls;
72
73 for (;;) {
74 int c, optidx = 0;
75 enum {
76 ARG_UPDATE = 257,
77 ARG_UPDATE_ONLY = 258,
78 ARG_MTU,
79 ARG_MPU,
80 ARG_OVERHEAD,
81 ARG_LINKTYPE,
82 ARG_PROTO,
83 ARG_PRIO,
84 };
85 static struct option long_opts[] = {
86 { "quiet", 0, 0, 'q' },
87 { "help", 0, 0, 'h' },
88 { "version", 0, 0, 'v' },
89 { "dev", 1, 0, 'd' },
90 { "parent", 1, 0, 'p' },
91 { "id", 1, 0, 'i' },
92 { "proto", 1, 0, ARG_PROTO },
93 { "prio", 1, 0, ARG_PRIO },
94 { "update", 0, 0, ARG_UPDATE },
95 { "update-only", 0, 0, ARG_UPDATE_ONLY },
96 { "mtu", 1, 0, ARG_MTU },
97 { "mpu", 1, 0, ARG_MPU },
98 { "overhead", 1, 0, ARG_OVERHEAD },
99 { "linktype", 1, 0, ARG_LINKTYPE },
100 { 0, 0, 0, 0 }
101 };
102
103 c = getopt_long(argc, argv, "+qhvd:p:i:",
104 long_opts, &optidx);
105 if (c == -1)
106 break;
107
108 switch (c) {
109 case 'q': quiet = 1; break;
110 case 'h': print_usage(); break;
111 case 'v': nl_cli_print_version(); break;
112 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
113 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
114 case 'i': free(id); id = strdup(optarg); break;
115 case ARG_UPDATE: flags = NLM_F_CREATE; break;
116 case ARG_UPDATE_ONLY: flags = 0; break;
117 case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
118 case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break;
119 case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break;
120 case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break;
121 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
122 case ARG_PRIO:
123 rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
124 break;
125 }
126 }
127
128 if (optind >= argc)
129 print_usage();
130
131 if (!rtnl_tc_get_ifindex(tc))
132 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
133
134 if (!rtnl_tc_get_parent(tc))
135 nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
136
137 if (id) {
138 nl_cli_tc_parse_handle(tc, id, 1);
139 free(id);
140 }
141
142 kind = argv[optind++];
143 rtnl_tc_set_kind(tc, kind);
144
145 if (!(ops = rtnl_tc_get_ops(tc)))
146 nl_cli_fatal(ENOENT, "Unknown classifier \"%s\".", kind);
147
148 if (!(tm = nl_cli_tc_lookup(ops)))
149 nl_cli_fatal(ENOTSUP, "Classifier type \"%s\" not supported.", kind);
150
151 tm->tm_parse_argv(tc, argc, argv);
152
153 if (!quiet) {
154 printf("Adding ");
155 nl_object_dump(OBJ_CAST(cls), &dp);
156 }
157
158 if ((err = rtnl_cls_add(sock, cls, flags)) < 0)
159 nl_cli_fatal(EINVAL, "Unable to add classifier: %s", nl_geterror(err));
160
161 return 0;
162 }
163