12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/tc.h>
14 #include <netlink/cli/qdisc.h>
15 #include <netlink/cli/class.h>
16 #include <netlink/cli/link.h>
20 static void print_usage(
void)
23 "Usage: nl-class-add [OPTIONS]... class [CONFIGURATION]...\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 class if it exists.\n"
30 " --update-only Only update class, never create it.\n"
31 " -d, --dev=DEV Network device the class should be attached to.\n"
32 " -i, --id=ID ID of new class (default: auto-generated)\n"
33 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
34 " --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
35 " --mpu=SIZE Minimum packet size on the link (default: 0).\n"
36 " --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
37 " --linktype=TYPE Overwrite linktype (default: type of network device)\n"
40 " -h, --help Show help text of class specific options.\n"
43 " $ nl-class-add --dev=eth1 --parent=root htb --rate=100mbit\n"
49 int main(
int argc,
char *argv[])
52 struct rtnl_class *
class;
54 struct nl_cache *link_cache;
61 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
62 char *kind, *
id = NULL;
64 sock = nl_cli_alloc_socket();
65 nl_cli_connect(sock, NETLINK_ROUTE);
67 link_cache = nl_cli_link_alloc_cache(sock);
69 class = nl_cli_class_alloc();
70 tc = (
struct rtnl_tc *)
class;
76 ARG_UPDATE_ONLY = 258,
82 static struct option long_opts[] = {
83 {
"quiet", 0, 0,
'q' },
84 {
"help", 0, 0,
'h' },
85 {
"version", 0, 0,
'v' },
87 {
"parent", 1, 0,
'p' },
89 {
"update", 0, 0, ARG_UPDATE },
90 {
"update-only", 0, 0, ARG_UPDATE_ONLY },
91 {
"mtu", 1, 0, ARG_MTU },
92 {
"mpu", 1, 0, ARG_MPU },
93 {
"overhead", 1, 0, ARG_OVERHEAD },
94 {
"linktype", 1, 0, ARG_LINKTYPE },
98 c = getopt_long(argc, argv,
"+qhvd:p:i:",
104 case 'q': quiet = 1;
break;
105 case 'h': print_usage();
break;
106 case 'v': nl_cli_print_version();
break;
107 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg);
break;
108 case 'p': nl_cli_tc_parse_parent(tc, optarg);
break;
109 case 'i':
id = strdup(optarg);
break;
110 case ARG_UPDATE: flags = NLM_F_CREATE;
break;
111 case ARG_UPDATE_ONLY: flags = 0;
break;
112 case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg);
break;
113 case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg);
break;
114 case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg);
break;
115 case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg);
break;
123 nl_cli_fatal(EINVAL,
"You must specify a network device (--dev=XXX)");
126 nl_cli_fatal(EINVAL,
"You must specify a parent (--parent=XXX)");
129 nl_cli_tc_parse_handle(tc,
id, 1);
133 kind = argv[optind++];
136 if (!(ops = rtnl_tc_get_ops(tc)))
139 if (!(tm = nl_cli_tc_lookup(ops)))
140 nl_cli_fatal(ENOTSUP,
"class type \"%s\" not supported.", kind);
142 tm->tm_parse_argv(tc, argc, argv);
150 nl_cli_fatal(EINVAL,
"Unable to add class: %s", nl_geterror(err));