libnl  3.5.0
tc.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/lib/tc.c CLI Traffic Control Helpers
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-private/route/tc-api.h>
16 
17 /**
18  * @ingroup cli
19  * @defgroup cli_tc Traffic Control
20  * @{
21  */
22 void nl_cli_tc_parse_dev(struct rtnl_tc *tc, struct nl_cache *link_cache, char *name)
23 {
24  struct rtnl_link *link;
25 
26  link = rtnl_link_get_by_name(link_cache, name);
27  if (!link)
28  nl_cli_fatal(ENOENT, "Link \"%s\" does not exist.", name);
29 
30  rtnl_tc_set_link(tc, link);
31  rtnl_link_put(link);
32 }
33 
34 void nl_cli_tc_parse_parent(struct rtnl_tc *tc, char *arg)
35 {
36  uint32_t parent;
37  int err;
38 
39  if ((err = rtnl_tc_str2handle(arg, &parent)) < 0)
40  nl_cli_fatal(err, "Unable to parse handle \"%s\": %s",
41  arg, nl_geterror(err));
42 
43  rtnl_tc_set_parent(tc, parent);
44 }
45 
46 void nl_cli_tc_parse_handle(struct rtnl_tc *tc, char *arg, int create)
47 {
48  uint32_t handle, parent;
49  int err;
50 
51  parent = rtnl_tc_get_parent(tc);
52 
53  if ((err = rtnl_tc_str2handle(arg, &handle)) < 0) {
54  if (err == -NLE_OBJ_NOTFOUND && create)
55  err = rtnl_classid_generate(arg, &handle, parent);
56 
57  if (err < 0)
58  nl_cli_fatal(err, "Unable to parse handle \"%s\": %s",
59  arg, nl_geterror(err));
60  }
61 
62  rtnl_tc_set_handle(tc, handle);
63 }
64 
65 void nl_cli_tc_parse_mtu(struct rtnl_tc *tc, char *arg)
66 {
68 }
69 
70 void nl_cli_tc_parse_mpu(struct rtnl_tc *tc, char *arg)
71 {
73 }
74 
75 void nl_cli_tc_parse_overhead(struct rtnl_tc *tc, char *arg)
76 {
78 }
79 
80 void nl_cli_tc_parse_kind(struct rtnl_tc *tc, char *arg)
81 {
82  rtnl_tc_set_kind(tc, arg);
83 }
84 
85 void nl_cli_tc_parse_linktype(struct rtnl_tc *tc, char *arg)
86 {
87  int type;
88 
89  if ((type = nl_str2llproto(arg)) < 0)
90  nl_cli_fatal(type, "Unable to parse linktype \"%s\": %s",
91  arg, nl_geterror(type));
92 
93  rtnl_tc_set_linktype(tc, type);
94 }
95 
96 static NL_LIST_HEAD(tc_modules);
97 
98 static struct nl_cli_tc_module *__nl_cli_tc_lookup(struct rtnl_tc_ops *ops)
99 {
100  struct nl_cli_tc_module *tm;
101 
102  nl_list_for_each_entry(tm, &tc_modules, tm_list)
103  if (tm->tm_ops == ops)
104  return tm;
105 
106  return NULL;
107 }
108 
109 struct nl_cli_tc_module *nl_cli_tc_lookup(struct rtnl_tc_ops *ops)
110 {
111  struct nl_cli_tc_module *tm;
112 
113  if ((tm = __nl_cli_tc_lookup(ops)))
114  return tm;
115 
116  switch (ops->to_type) {
117  case RTNL_TC_TYPE_QDISC:
118  case RTNL_TC_TYPE_CLASS:
119  nl_cli_load_module("cli/qdisc", ops->to_kind);
120  break;
121 
122  case RTNL_TC_TYPE_CLS:
123  nl_cli_load_module("cli/cls", ops->to_kind);
124  break;
125 
126  default:
127  nl_cli_fatal(EINVAL, "BUG: unhandled TC object type %d",
128  ops->to_type);
129  }
130 
131  if (!(tm = __nl_cli_tc_lookup(ops))) {
132  nl_cli_fatal(EINVAL, "Application bug: The shared library for "
133  "the tc object \"%s\" was successfully loaded but it "
134  "seems that module did not register itself",
135  ops->to_kind);
136  }
137 
138  return tm;
139 }
140 
141 void nl_cli_tc_register(struct nl_cli_tc_module *tm)
142 {
143  struct rtnl_tc_ops *ops;
144 
145  if (!(ops = rtnl_tc_lookup_ops(tm->tm_type, tm->tm_name))) {
146  nl_cli_fatal(ENOENT, "Unable to register CLI TC module "
147  "\"%s\": No matching libnl TC module found.", tm->tm_name);
148  }
149 
150  if (__nl_cli_tc_lookup(ops)) {
151  nl_cli_fatal(EEXIST, "Unable to register CLI TC module "
152  "\"%s\": Module already registered.", tm->tm_name);
153  }
154 
155  tm->tm_ops = ops;
156 
157  nl_list_add_tail(&tm->tm_list, &tc_modules);
158 }
159 
160 void nl_cli_tc_unregister(struct nl_cli_tc_module *tm)
161 {
162  nl_list_del(&tm->tm_list);
163 }
164 
165 
166 /** @} */
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
Definition: tc.c:530
int rtnl_tc_str2handle(const char *str, uint32_t *res)
Convert a charactering strint to a traffic control handle.
Definition: classid.c:155
void rtnl_tc_set_linktype(struct rtnl_tc *tc, uint32_t type)
Set the linktype of a traffic control object.
Definition: tc.c:457
void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
Set the parent identifier of a traffic control object.
Definition: tc.c:508
void rtnl_tc_set_link(struct rtnl_tc *tc, struct rtnl_link *link)
Set link of traffic control object.
Definition: tc.c:311
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:518
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:43
void rtnl_tc_set_handle(struct rtnl_tc *tc, uint32_t id)
Set identifier of traffic control object.
Definition: tc.c:487
void rtnl_tc_set_mpu(struct rtnl_tc *tc, uint32_t mpu)
Set the Minimum Packet Unit (MPU) of a traffic control object.
Definition: tc.c:400
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
void rtnl_tc_set_mtu(struct rtnl_tc *tc, uint32_t mtu)
Set the Maximum Transmission Unit (MTU) of traffic control object.
Definition: tc.c:365
void rtnl_tc_set_overhead(struct rtnl_tc *tc, uint32_t overhead)
Set per packet overhead of a traffic control object.
Definition: tc.c:429