libnl  3.6.0
cls.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2010-2011 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 /**
7  * @ingroup cli
8  * @defgroup cli_cls Classifiers
9  * @{
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/cls.h>
14 #include <netlink/route/cls/ematch.h>
15 
16 struct rtnl_cls *nl_cli_cls_alloc(void)
17 {
18  struct rtnl_cls *cls;
19 
20  if (!(cls = rtnl_cls_alloc()))
21  nl_cli_fatal(ENOMEM, "Unable to allocate classifier object");
22 
23  return cls;
24 }
25 
26 struct nl_cache *nl_cli_cls_alloc_cache(struct nl_sock *sock, int ifindex,
27  uint32_t parent)
28 {
29  struct nl_cache *cache;
30  int err;
31 
32  if ((err = rtnl_cls_alloc_cache(sock, ifindex, parent, &cache)) < 0)
33  nl_cli_fatal(err, "Unable to allocate classifier cache: %s",
34  nl_geterror(err));
35 
36  return cache;
37 }
38 
39 void nl_cli_cls_parse_proto(struct rtnl_cls *cls, char *arg)
40 {
41  int proto;
42 
43  if ((proto = nl_str2ether_proto(arg)) < 0)
44  nl_cli_fatal(proto, "Unknown protocol \"%s\".", arg);
45 
46  rtnl_cls_set_protocol(cls, proto);
47 }
48 
49 struct rtnl_ematch_tree *nl_cli_cls_parse_ematch(struct rtnl_cls *cls, char *arg)
50 {
51  struct rtnl_ematch_tree *tree;
52  char *errstr = NULL;
53  int err;
54 
55  if ((err = rtnl_ematch_parse_expr(arg, &errstr, &tree)) < 0)
56  nl_cli_fatal(err, "Unable to parse ematch expression: %s",
57  errstr);
58 
59  if (errstr)
60  free(errstr);
61 
62  return tree;
63 }
64 
65 /** @} */
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
int rtnl_cls_alloc_cache(struct nl_sock *sk, int ifindex, uint32_t parent, struct nl_cache **result)
Allocate a cache and fill it with all configured classifiers.
Definition: cls.c:321