tizen 2.3 release
[kernel/api/system-resource.git] / src / network / include / nl-helper.h
1 /*
2  * resourced
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /** @file nl-helper.h
21  *
22  *  @desc Common netlink helper function
23  *
24  *  Created on: Jun 25, 2012
25  */
26
27 #ifndef RESOURCED_NL_HELPER_H_
28 #define RESOURCED_NL_HELPER_H_
29
30 #include "app-stat.h"
31 /*#include "nfacct-helper.h"*/
32
33 #include <sys/socket.h>
34 #include <linux/netlink.h>
35 #include <linux/genetlink.h>
36 #include <linux/rtnetlink.h>
37
38 #define NLA_BUF_MAX 65560       /*(65 * 1024) - used in tc_common,
39          we'll do the same */
40
41 /*TODO: move to common place and rewrite because it's from TC*/
42 #define NLMSG_TAIL(nmsg) \
43         ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
44
45 /*TODO remove unused code */
46 typedef struct {
47         struct nlmsghdr n;
48         struct tcmsg t;
49         char buf[NLA_BUF_MAX];
50 } rt_param;
51
52 void put_attr(rt_param *arg, int type, const void *data, int data_len);
53
54 /*
55  * Generic macros for dealing with netlink sockets. Might be duplicated
56  * elsewhere. It is recommended that commercial grade applications use
57  * libnl or libnetlink and use the interfaces provided by the library
58  */
59 #define GENLMSG_PAYLOAD(glh) (NLMSG_PAYLOAD(glh, 0) - GENL_HDRLEN)
60 #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
61 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
62
63 #define NETLINK_BUF_SIZE 16536
64
65 enum nfnl_acct_msg_types {
66         NFNL_MSG_ACCT_NEW,
67         NFNL_MSG_ACCT_GET,
68         NFNL_MSG_ACCT_GET_CTRZERO,
69         NFNL_MSG_ACCT_DEL,
70         NFNL_MSG_ACCT_MAX
71 };
72
73 enum nfnl_acct_type {
74         NFACCT_UNSPEC,
75         NFACCT_NAME,
76         NFACCT_PKTS,
77         NFACCT_BYTES,
78         NFACCT_USE,
79         NFACCT_FLAGS,
80         NFACCT_QUOTA,
81         NFACCT_FILTER,
82         __NFACCT_MAX
83 };
84
85 enum nfnl_attr_filter_type {
86         NFACCT_FILTER_ATTR_UNSPEC,
87         NFACCT_FILTER_ATTR_MASK,
88         NFACCT_FILTER_ATTR_VALUE,
89         __NFACCT_FILTER_ATTR_MAX
90 };
91
92 #define NFACCT_MAX (__NFACCT_MAX - 1)
93
94 struct genl {
95         struct nlmsghdr n;
96         struct genlmsghdr g;
97         char buf[NETLINK_BUF_SIZE];
98 };
99
100 struct netlink_serialization_params {
101         traffic_stat_tree *stat_tree;
102         struct genl *ans;
103         struct counter_arg *carg;
104         int (*eval_attr)(struct rtattr *attr_list[__NFACCT_MAX],
105                 void *user_data);
106         int (*post_eval_attr)(void *user_data);
107 };
108
109 typedef struct {
110         void (*deserialize_answer)(struct netlink_serialization_params *params);
111         void (*finalize)(struct netlink_serialization_params *params);
112         struct netlink_serialization_params params;
113 } netlink_serialization_command;
114
115 int create_netlink(int protocol, uint32_t groups);
116 int read_netlink(int sock, void *buf, size_t len);
117
118 void fill_attribute_list(struct rtattr **atb, const int max_len,
119         struct rtattr *rt_na, int rt_len);
120
121 #endif  /* RESOURCED_NL_HELPER_H_ */