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