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