• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nl-class-add.c     Add/Update/Replace Traffic Class
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU Lesser General Public
7  *	License as published by the Free Software Foundation version 2.1
8  *	of the License.
9  *
10  * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/tc.h>
15 #include <netlink/cli/qdisc.h>
16 #include <netlink/cli/class.h>
17 #include <netlink/cli/link.h>
18 
19 #include <netlink-private/route/tc-api.h>
20 
21 #include <linux/netlink.h>
22 
23 static int quiet = 0;
24 
print_usage(void)25 static void print_usage(void)
26 {
27 	printf(
28 "Usage: nl-class-add [OPTIONS]... class [CONFIGURATION]...\n"
29 "\n"
30 "OPTIONS\n"
31 " -q, --quiet               Do not print informal notifications.\n"
32 " -h, --help                Show this help text.\n"
33 " -v, --version             Show versioning information.\n"
34 "     --update              Update class if it exists.\n"
35 "     --update-only         Only update class, never create it.\n"
36 " -d, --dev=DEV             Network device the class should be attached to.\n"
37 " -i, --id=ID               ID of new class (default: auto-generated)\n"
38 " -p, --parent=ID           ID of parent { root | ingress | class-ID }\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 class specific options.\n"
46 "\n"
47 "EXAMPLE\n"
48 "   $ nl-class-add --dev=eth1 --parent=root htb --rate=100mbit\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_class *class;
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 	class = nl_cli_class_alloc();
75 	tc = (struct rtnl_tc *) class;
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 		};
87 		static struct option long_opts[] = {
88 			{ "quiet", 0, 0, 'q' },
89 			{ "help", 0, 0, 'h' },
90 			{ "version", 0, 0, 'v' },
91 			{ "dev", 1, 0, 'd' },
92 			{ "parent", 1, 0, 'p' },
93 			{ "id", 1, 0, 'i' },
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': 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 		}
122 	}
123 
124 	if (optind >= argc)
125 		print_usage();
126 
127 	if (!rtnl_tc_get_ifindex(tc))
128 		nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
129 
130 	if (!rtnl_tc_get_parent(tc))
131 		nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
132 
133 	if (id) {
134 		nl_cli_tc_parse_handle(tc, id, 1);
135 		free(id);
136 	}
137 
138 	kind = argv[optind++];
139 	rtnl_tc_set_kind(tc, kind);
140 
141 	if (!(ops = rtnl_tc_get_ops(tc)))
142 		nl_cli_fatal(ENOENT, "Unknown class \"%s\"", kind);
143 
144 	if (!(tm = nl_cli_tc_lookup(ops)))
145 		nl_cli_fatal(ENOTSUP, "class type \"%s\" not supported.", kind);
146 
147 	tm->tm_parse_argv(tc, argc, argv);
148 
149 	if (!quiet) {
150 		printf("Adding ");
151 		nl_object_dump(OBJ_CAST(class), &dp);
152 	}
153 
154 	if ((err = rtnl_class_add(sock, class, flags)) < 0)
155 		nl_cli_fatal(EINVAL, "Unable to add class: %s", nl_geterror(err));
156 
157 	return 0;
158 }
159