libnl  3.2.11
nl-cls-add.c
1 /*
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 static int quiet = 0;
17 
18 static void print_usage(void)
19 {
20  printf(
21 "Usage: nl-cls-add [OPTIONS]... classifier [CONFIGURATION]...\n"
22 "\n"
23 "OPTIONS\n"
24 " -q, --quiet Do not print informal notifications.\n"
25 " -h, --help Show this help text.\n"
26 " -v, --version Show versioning information.\n"
27 " --update Update classifier if it exists.\n"
28 " --update-only Only update classifier, never create it.\n"
29 " -d, --dev=DEV Network device the classifier should be attached to.\n"
30 " -i, --id=ID ID of new classifier (default: auto-generated)\n"
31 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
32 " --protocol=PROTO Protocol to match (default: all)\n"
33 " --prio=PRIO Priority (default: 0)\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"
38 "\n"
39 "CONFIGURATION\n"
40 " -h, --help Show help text of classifier specific options.\n"
41 "\n"
42 "EXAMPLE\n"
43 " $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
44 "\n"
45  );
46  exit(0);
47 }
48 
49 int main(int argc, char *argv[])
50 {
51  struct nl_sock *sock;
52  struct rtnl_cls *cls;
53  struct rtnl_tc *tc;
54  struct nl_cache *link_cache;
55  struct nl_dump_params dp = {
57  .dp_fd = stdout,
58  };
59  struct nl_cli_tc_module *tm;
60  struct rtnl_tc_ops *ops;
61  int err, flags = NLM_F_CREATE | NLM_F_EXCL;
62  char *kind, *id = NULL;
63 
64  sock = nl_cli_alloc_socket();
65  nl_cli_connect(sock, NETLINK_ROUTE);
66 
67  link_cache = nl_cli_link_alloc_cache(sock);
68 
69  cls = nl_cli_cls_alloc();
70  tc = (struct rtnl_tc *) cls;
71 
72  for (;;) {
73  int c, optidx = 0;
74  enum {
75  ARG_UPDATE = 257,
76  ARG_UPDATE_ONLY = 258,
77  ARG_MTU,
78  ARG_MPU,
79  ARG_OVERHEAD,
80  ARG_LINKTYPE,
81  ARG_PROTO,
82  ARG_PRIO,
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  { "proto", 1, 0, ARG_PROTO },
92  { "prio", 1, 0, ARG_PRIO },
93  { "update", 0, 0, ARG_UPDATE },
94  { "update-only", 0, 0, ARG_UPDATE_ONLY },
95  { "mtu", 1, 0, ARG_MTU },
96  { "mpu", 1, 0, ARG_MPU },
97  { "overhead", 1, 0, ARG_OVERHEAD },
98  { "linktype", 1, 0, ARG_LINKTYPE },
99  { 0, 0, 0, 0 }
100  };
101 
102  c = getopt_long(argc, argv, "+qhvd:p:i:",
103  long_opts, &optidx);
104  if (c == -1)
105  break;
106 
107  switch (c) {
108  case 'q': quiet = 1; break;
109  case 'h': print_usage(); break;
110  case 'v': nl_cli_print_version(); break;
111  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
112  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
113  case 'i': id = strdup(optarg); break;
114  case ARG_UPDATE: flags = NLM_F_CREATE; break;
115  case ARG_UPDATE_ONLY: flags = 0; break;
116  case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
117  case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break;
118  case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break;
119  case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break;
120  case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
121  case ARG_PRIO:
122  rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
123  break;
124  }
125  }
126 
127  if (optind >= argc)
128  print_usage();
129 
130  if (!rtnl_tc_get_ifindex(tc))
131  nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
132 
133  if (!rtnl_tc_get_parent(tc))
134  nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
135 
136  if (id) {
137  nl_cli_tc_parse_handle(tc, id, 1);
138  free(id);
139  }
140 
141  kind = argv[optind++];
142  rtnl_tc_set_kind(tc, kind);
143 
144  if (!(ops = rtnl_tc_get_ops(tc)))
145  nl_cli_fatal(ENOENT, "Unknown classifier \"%s\".", kind);
146 
147  if (!(tm = nl_cli_tc_lookup(ops)))
148  nl_cli_fatal(ENOTSUP, "Classifier type \"%s\" not supported.", kind);
149 
150  tm->tm_parse_argv(tc, argc, argv);
151 
152  if (!quiet) {
153  printf("Adding ");
154  nl_object_dump(OBJ_CAST(cls), &dp);
155  }
156 
157  if ((err = rtnl_cls_add(sock, cls, flags)) < 0)
158  nl_cli_fatal(EINVAL, "Unable to add classifier: %s", nl_geterror(err));
159 
160  return 0;
161 }