libnl  3.3.0
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 #include <netlink-private/route/tc-api.h>
17 
18 #include <linux/netlink.h>
19 
20 static int quiet = 0;
21 
22 static void print_usage(void)
23 {
24  printf(
25 "Usage: nl-cls-add [OPTIONS]... classifier [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 classifier if it exists.\n"
32 " --update-only Only update classifier, never create it.\n"
33 " -d, --dev=DEV Network device the classifier should be attached to.\n"
34 " -i, --id=ID ID of new classifier (default: auto-generated)\n"
35 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
36 " --proto=PROTO Protocol to match (default: all)\n"
37 " --prio=PRIO Priority (default: 0)\n"
38 " --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
39 " --mpu=SIZE Minimum packet size on the link (default: 0).\n"
40 " --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
41 " --linktype=TYPE Overwrite linktype (default: type of network device)\n"
42 "\n"
43 "CONFIGURATION\n"
44 " -h, --help Show help text of classifier specific options.\n"
45 "\n"
46 "EXAMPLE\n"
47 " $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
48 "\n"
49  );
50  exit(0);
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  struct nl_sock *sock;
56  struct rtnl_cls *cls;
57  struct rtnl_tc *tc;
58  struct nl_cache *link_cache;
59  struct nl_dump_params dp = {
61  .dp_fd = stdout,
62  };
63  struct nl_cli_tc_module *tm;
64  struct rtnl_tc_ops *ops;
65  int err, flags = NLM_F_CREATE | NLM_F_EXCL;
66  char *kind, *id = NULL;
67 
68  sock = nl_cli_alloc_socket();
69  nl_cli_connect(sock, NETLINK_ROUTE);
70 
71  link_cache = nl_cli_link_alloc_cache(sock);
72 
73  cls = nl_cli_cls_alloc();
74  tc = (struct rtnl_tc *) cls;
75 
76  for (;;) {
77  int c, optidx = 0;
78  enum {
79  ARG_UPDATE = 257,
80  ARG_UPDATE_ONLY = 258,
81  ARG_MTU,
82  ARG_MPU,
83  ARG_OVERHEAD,
84  ARG_LINKTYPE,
85  ARG_PROTO,
86  ARG_PRIO,
87  };
88  static struct option long_opts[] = {
89  { "quiet", 0, 0, 'q' },
90  { "help", 0, 0, 'h' },
91  { "version", 0, 0, 'v' },
92  { "dev", 1, 0, 'd' },
93  { "parent", 1, 0, 'p' },
94  { "id", 1, 0, 'i' },
95  { "proto", 1, 0, ARG_PROTO },
96  { "prio", 1, 0, ARG_PRIO },
97  { "update", 0, 0, ARG_UPDATE },
98  { "update-only", 0, 0, ARG_UPDATE_ONLY },
99  { "mtu", 1, 0, ARG_MTU },
100  { "mpu", 1, 0, ARG_MPU },
101  { "overhead", 1, 0, ARG_OVERHEAD },
102  { "linktype", 1, 0, ARG_LINKTYPE },
103  { 0, 0, 0, 0 }
104  };
105 
106  c = getopt_long(argc, argv, "+qhvd:p:i:",
107  long_opts, &optidx);
108  if (c == -1)
109  break;
110 
111  switch (c) {
112  case 'q': quiet = 1; break;
113  case 'h': print_usage(); break;
114  case 'v': nl_cli_print_version(); break;
115  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
116  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
117  case 'i': id = strdup(optarg); break;
118  case ARG_UPDATE: flags = NLM_F_CREATE; break;
119  case ARG_UPDATE_ONLY: flags = 0; break;
120  case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
121  case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break;
122  case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break;
123  case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break;
124  case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
125  case ARG_PRIO:
126  rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
127  break;
128  }
129  }
130 
131  if (optind >= argc)
132  print_usage();
133 
134  if (!rtnl_tc_get_ifindex(tc))
135  nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
136 
137  if (!rtnl_tc_get_parent(tc))
138  nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
139 
140  if (id) {
141  nl_cli_tc_parse_handle(tc, id, 1);
142  free(id);
143  }
144 
145  kind = argv[optind++];
146  rtnl_tc_set_kind(tc, kind);
147 
148  if (!(ops = rtnl_tc_get_ops(tc)))
149  nl_cli_fatal(ENOENT, "Unknown classifier \"%s\".", kind);
150 
151  if (!(tm = nl_cli_tc_lookup(ops)))
152  nl_cli_fatal(ENOTSUP, "Classifier type \"%s\" not supported.", kind);
153 
154  tm->tm_parse_argv(tc, argc, argv);
155 
156  if (!quiet) {
157  printf("Adding ");
158  nl_object_dump(OBJ_CAST(cls), &dp);
159  }
160 
161  if ((err = rtnl_cls_add(sock, cls, flags)) < 0)
162  nl_cli_fatal(EINVAL, "Unable to add classifier: %s", nl_geterror(err));
163 
164  return 0;
165 }
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
Definition: tc.c:511
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
int rtnl_cls_add(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
Add/Update classifier.
Definition: cls.c:184
Dump all attributes but no statistics.
Definition: types.h:23
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:499
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
Dumping parameters.
Definition: types.h:33
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:275