1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/tc.h>
8 #include <netlink-private/route/tc-api.h>
9
10 /**
11 * @ingroup cli
12 * @defgroup cli_tc Traffic Control
13 * @{
14 */
nl_cli_tc_parse_dev(struct rtnl_tc * tc,struct nl_cache * link_cache,char * name)15 void nl_cli_tc_parse_dev(struct rtnl_tc *tc, struct nl_cache *link_cache, char *name)
16 {
17 struct rtnl_link *link;
18
19 link = rtnl_link_get_by_name(link_cache, name);
20 if (!link)
21 nl_cli_fatal(ENOENT, "Link \"%s\" does not exist.", name);
22
23 rtnl_tc_set_link(tc, link);
24 rtnl_link_put(link);
25 }
26
nl_cli_tc_parse_parent(struct rtnl_tc * tc,char * arg)27 void nl_cli_tc_parse_parent(struct rtnl_tc *tc, char *arg)
28 {
29 uint32_t parent;
30 int err;
31
32 if ((err = rtnl_tc_str2handle(arg, &parent)) < 0)
33 nl_cli_fatal(err, "Unable to parse handle \"%s\": %s",
34 arg, nl_geterror(err));
35
36 rtnl_tc_set_parent(tc, parent);
37 }
38
nl_cli_tc_parse_handle(struct rtnl_tc * tc,char * arg,int create)39 void nl_cli_tc_parse_handle(struct rtnl_tc *tc, char *arg, int create)
40 {
41 uint32_t handle, parent;
42 int err;
43
44 parent = rtnl_tc_get_parent(tc);
45
46 if ((err = rtnl_tc_str2handle(arg, &handle)) < 0) {
47 if (err == -NLE_OBJ_NOTFOUND && create)
48 err = rtnl_classid_generate(arg, &handle, parent);
49
50 if (err < 0)
51 nl_cli_fatal(err, "Unable to parse handle \"%s\": %s",
52 arg, nl_geterror(err));
53 }
54
55 rtnl_tc_set_handle(tc, handle);
56 }
57
nl_cli_tc_parse_mtu(struct rtnl_tc * tc,char * arg)58 void nl_cli_tc_parse_mtu(struct rtnl_tc *tc, char *arg)
59 {
60 rtnl_tc_set_mtu(tc, nl_cli_parse_u32(arg));
61 }
62
nl_cli_tc_parse_mpu(struct rtnl_tc * tc,char * arg)63 void nl_cli_tc_parse_mpu(struct rtnl_tc *tc, char *arg)
64 {
65 rtnl_tc_set_mpu(tc, nl_cli_parse_u32(arg));
66 }
67
nl_cli_tc_parse_overhead(struct rtnl_tc * tc,char * arg)68 void nl_cli_tc_parse_overhead(struct rtnl_tc *tc, char *arg)
69 {
70 rtnl_tc_set_overhead(tc, nl_cli_parse_u32(arg));
71 }
72
nl_cli_tc_parse_kind(struct rtnl_tc * tc,char * arg)73 void nl_cli_tc_parse_kind(struct rtnl_tc *tc, char *arg)
74 {
75 rtnl_tc_set_kind(tc, arg);
76 }
77
nl_cli_tc_parse_linktype(struct rtnl_tc * tc,char * arg)78 void nl_cli_tc_parse_linktype(struct rtnl_tc *tc, char *arg)
79 {
80 int type;
81
82 if ((type = nl_str2llproto(arg)) < 0)
83 nl_cli_fatal(type, "Unable to parse linktype \"%s\": %s",
84 arg, nl_geterror(type));
85
86 rtnl_tc_set_linktype(tc, type);
87 }
88
89 static NL_LIST_HEAD(tc_modules);
90
__nl_cli_tc_lookup(struct rtnl_tc_ops * ops)91 static struct nl_cli_tc_module *__nl_cli_tc_lookup(struct rtnl_tc_ops *ops)
92 {
93 struct nl_cli_tc_module *tm;
94
95 nl_list_for_each_entry(tm, &tc_modules, tm_list)
96 if (tm->tm_ops == ops)
97 return tm;
98
99 return NULL;
100 }
101
nl_cli_tc_lookup(struct rtnl_tc_ops * ops)102 struct nl_cli_tc_module *nl_cli_tc_lookup(struct rtnl_tc_ops *ops)
103 {
104 struct nl_cli_tc_module *tm;
105
106 if ((tm = __nl_cli_tc_lookup(ops)))
107 return tm;
108
109 switch (ops->to_type) {
110 case RTNL_TC_TYPE_QDISC:
111 case RTNL_TC_TYPE_CLASS:
112 nl_cli_load_module("cli/qdisc", ops->to_kind);
113 break;
114
115 case RTNL_TC_TYPE_CLS:
116 nl_cli_load_module("cli/cls", ops->to_kind);
117 break;
118
119 default:
120 nl_cli_fatal(EINVAL, "BUG: unhandled TC object type %d",
121 ops->to_type);
122 }
123
124 if (!(tm = __nl_cli_tc_lookup(ops))) {
125 nl_cli_fatal(EINVAL, "Application bug: The shared library for "
126 "the tc object \"%s\" was successfully loaded but it "
127 "seems that module did not register itself",
128 ops->to_kind);
129 }
130
131 return tm;
132 }
133
nl_cli_tc_register(struct nl_cli_tc_module * tm)134 void nl_cli_tc_register(struct nl_cli_tc_module *tm)
135 {
136 struct rtnl_tc_ops *ops;
137
138 if (!(ops = rtnl_tc_lookup_ops(tm->tm_type, tm->tm_name))) {
139 nl_cli_fatal(ENOENT, "Unable to register CLI TC module "
140 "\"%s\": No matching libnl TC module found.", tm->tm_name);
141 }
142
143 if (__nl_cli_tc_lookup(ops)) {
144 nl_cli_fatal(EEXIST, "Unable to register CLI TC module "
145 "\"%s\": Module already registered.", tm->tm_name);
146 }
147
148 tm->tm_ops = ops;
149
150 nl_list_add_tail(&tm->tm_list, &tc_modules);
151 }
152
nl_cli_tc_unregister(struct nl_cli_tc_module * tm)153 void nl_cli_tc_unregister(struct nl_cli_tc_module *tm)
154 {
155 nl_list_del(&tm->tm_list);
156 }
157
158
159 /** @} */
160