lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * src/nl-class-add.c Add/Update/Replace Traffic Class |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation version 2.1 |
| 7 | * of the License. |
| 8 | * |
| 9 | * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch> |
| 10 | */ |
| 11 | |
| 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> |
| 17 | |
| 18 | #include <netlink-private/route/tc-api.h> |
| 19 | |
| 20 | static int quiet = 0; |
| 21 | |
| 22 | static void print_usage(void) |
| 23 | { |
| 24 | printf( |
| 25 | "Usage: nl-class-add [OPTIONS]... class [CONFIGURATION]...\n" |
| 26 | "\n" |
| 27 | "OPTIONS\n" |
| 28 | " -q, --quiet Do not print informal notifications.\n" |
| 29 | " -h, --help Show this help text.\n" |
| 30 | " -v, --version Show versioning information.\n" |
| 31 | " --update Update class if it exists.\n" |
| 32 | " --update-only Only update class, never create it.\n" |
| 33 | " -d, --dev=DEV Network device the class should be attached to.\n" |
| 34 | " -i, --id=ID ID of new class (default: auto-generated)\n" |
| 35 | " -p, --parent=ID ID of parent { root | ingress | class-ID }\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 class specific options.\n" |
| 43 | "\n" |
| 44 | "EXAMPLE\n" |
| 45 | " $ nl-class-add --dev=eth1 --parent=root htb --rate=100mbit\n" |
| 46 | "\n" |
| 47 | ); |
| 48 | exit(0); |
| 49 | } |
| 50 | |
| 51 | int main(int argc, char *argv[]) |
| 52 | { |
| 53 | struct nl_sock *sock; |
| 54 | struct rtnl_class *class; |
| 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 | class = nl_cli_class_alloc(); |
| 72 | tc = (struct rtnl_tc *) class; |
| 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 | }; |
| 84 | static struct option long_opts[] = { |
| 85 | { "quiet", 0, 0, 'q' }, |
| 86 | { "help", 0, 0, 'h' }, |
| 87 | { "version", 0, 0, 'v' }, |
| 88 | { "dev", 1, 0, 'd' }, |
| 89 | { "parent", 1, 0, 'p' }, |
| 90 | { "id", 1, 0, 'i' }, |
| 91 | { "update", 0, 0, ARG_UPDATE }, |
| 92 | { "update-only", 0, 0, ARG_UPDATE_ONLY }, |
| 93 | { "mtu", 1, 0, ARG_MTU }, |
| 94 | { "mpu", 1, 0, ARG_MPU }, |
| 95 | { "overhead", 1, 0, ARG_OVERHEAD }, |
| 96 | { "linktype", 1, 0, ARG_LINKTYPE }, |
| 97 | { 0, 0, 0, 0 } |
| 98 | }; |
| 99 | |
| 100 | c = getopt_long(argc, argv, "+qhvd:p:i:", |
| 101 | long_opts, &optidx); |
| 102 | if (c == -1) |
| 103 | break; |
| 104 | |
| 105 | switch (c) { |
| 106 | case 'q': quiet = 1; break; |
| 107 | case 'h': print_usage(); break; |
| 108 | case 'v': nl_cli_print_version(); break; |
| 109 | case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break; |
| 110 | case 'p': nl_cli_tc_parse_parent(tc, optarg); break; |
| 111 | case 'i': id = strdup(optarg); break; |
| 112 | case ARG_UPDATE: flags = NLM_F_CREATE; break; |
| 113 | case ARG_UPDATE_ONLY: flags = 0; break; |
| 114 | case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break; |
| 115 | case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break; |
| 116 | case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break; |
| 117 | case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (optind >= argc) |
| 122 | print_usage(); |
| 123 | |
| 124 | if (!rtnl_tc_get_ifindex(tc)) |
| 125 | nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)"); |
| 126 | |
| 127 | if (!rtnl_tc_get_parent(tc)) |
| 128 | nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)"); |
| 129 | |
| 130 | if (id) { |
| 131 | nl_cli_tc_parse_handle(tc, id, 1); |
| 132 | free(id); |
| 133 | } |
| 134 | |
| 135 | kind = argv[optind++]; |
| 136 | rtnl_tc_set_kind(tc, kind); |
| 137 | |
| 138 | if (!(ops = rtnl_tc_get_ops(tc))) |
| 139 | nl_cli_fatal(ENOENT, "Unknown class \"%s\"", kind); |
| 140 | |
| 141 | if (!(tm = nl_cli_tc_lookup(ops))) |
| 142 | nl_cli_fatal(ENOTSUP, "class type \"%s\" not supported.", kind); |
| 143 | |
| 144 | tm->tm_parse_argv(tc, argc, argv); |
| 145 | |
| 146 | if (!quiet) { |
| 147 | printf("Adding "); |
| 148 | nl_object_dump(OBJ_CAST(class), &dp); |
| 149 | } |
| 150 | |
| 151 | if ((err = rtnl_class_add(sock, class, flags)) < 0) |
| 152 | nl_cli_fatal(EINVAL, "Unable to add class: %s", nl_geterror(err)); |
| 153 | |
| 154 | return 0; |
| 155 | } |