libnl: /data/src/libnl3/src/lib/cls.c Source File

libnl 3.10.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 "nl-default.h"
13
14#include <netlink/cli/utils.h>
15#include <netlink/cli/cls.h>
16#include <netlink/route/cls/ematch.h>
17
18struct rtnl_cls *nl_cli_cls_alloc(void)
19{
20 struct rtnl_cls *cls;
21
22 if (!(cls = rtnl_cls_alloc()))
23 nl_cli_fatal(ENOMEM, "Unable to allocate classifier object");
24
25 return cls;
26}
27
28struct nl_cache *nl_cli_cls_alloc_cache(struct nl_sock *sock, int ifindex,
29 uint32_t parent)
30{
31 struct nl_cache *cache;
32 int err;
33
34 if ((err = rtnl_cls_alloc_cache(sock, ifindex, parent, &cache)) < 0)
35 nl_cli_fatal(err, "Unable to allocate classifier cache: %s",
36 nl_geterror(err));
37
38 return cache;
39}
40
41void nl_cli_cls_parse_proto(struct rtnl_cls *cls, char *arg)
42{
43 int proto;
44
45 if ((proto = nl_str2ether_proto(arg)) < 0)
46 nl_cli_fatal(proto, "Unknown protocol \"%s\".", arg);
47
48 rtnl_cls_set_protocol(cls, proto);
49}
50
51struct rtnl_ematch_tree *nl_cli_cls_parse_ematch(struct rtnl_cls *cls, char *arg)
52{
53 struct rtnl_ematch_tree *tree;
54 char *errstr = NULL;
55 int err;
56
57 if ((err = rtnl_ematch_parse_expr(arg, &errstr, &tree)) < 0)
58 nl_cli_fatal(err, "Unable to parse ematch expression: %s",
59 errstr);
60
61 if (errstr)
62 free(errstr);
63
64 return tree;
65}
66
67/** @} */
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71
rtnl_cls_alloc_cache
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:331