2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <linux/netlink.h>
19 #include <vconf-keys.h>
21 #include "helper-nl.h"
22 #include "helper-nfacct-rule.h"
23 #include "helper-net-cls.h"
24 #include "helper-cgroup.h"
25 #include "helper-iptables.h"
27 #include "table-statistics.h"
28 #include "table-counters.h"
29 #include "stc-plugin-monitor.h"
30 #include "stc-plugin-monitor-connection.h"
31 #include "stc-plugin-monitor-rstn.h"
32 #include "stc-manager-plugin-exception.h"
34 static stc_system_s *g_system = NULL;
36 static int __vconf_get_int(const char *key, int *value)
40 ret = vconf_get_int(key, value);
41 if (ret != VCONF_OK) {
42 STC_LOGE("Failed to get vconfkey [%s] value", key);
49 static stc_error_e __close_contr_sock(stc_system_s *system)
51 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
53 /* close netlink socket for updating kernel counters */
54 if (system->contr_sock != -1) {
55 close(system->contr_sock);
56 system->contr_sock = -1;
59 if (system->contr_gsource_id != 0) {
60 g_source_remove(system->contr_gsource_id);
61 system->contr_gsource_id = 0;
64 return STC_ERROR_NONE;
67 static gboolean __process_contr_reply(GIOChannel *source,
68 GIOCondition condition,
71 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
73 GIOChannel *gio = NULL;
74 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
76 /* close netlink socket for updating kernel counters */
77 if (system->contr_sock != -1) {
78 close(system->contr_sock);
79 system->contr_sock = -1;
82 if (system->contr_gsource_id != 0) {
83 g_source_remove(system->contr_gsource_id);
84 system->contr_gsource_id = 0;
87 /* create netlink socket for updating kernel counters */
88 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
89 if (system->contr_sock < 0) {
90 STC_LOGE("failed to open socket");
92 return STC_ERROR_FAIL;
95 gio = g_io_channel_unix_new(system->contr_sock);
96 system->contr_gsource_id =
97 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
98 (GIOFunc) __process_contr_reply,
100 g_io_channel_unref(gio);
102 return STC_ERROR_NONE;
105 static void __check_rstn_limit_exceeded(gpointer data,
108 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
109 int32_t *limit_exceeded = (int32_t *)user_data;
111 if (rstn_data->limit_exceeded != 0)
112 *limit_exceeded = rstn_data->limit_exceeded;
115 static void __fill_nfacct_result(char *cnt_name, long long int bytes,
116 struct counter_arg *carg)
118 stc_monitor_rstn_reset_time_counters_if_required();
120 struct nfacct_rule counter = {
127 classid_bytes_context_s context = {
130 .data_limit_exceeded = FALSE,
133 if (!recreate_counter_by_name(cnt_name, &counter)) {
134 STC_LOGE("Can't parse counter name %s", cnt_name);
139 STC_LOGI("classid[\033[1;36m%u\033[0;m] iftype[%u] "
140 "iotype[%d] intend[%d] ifname[%s] bytes[%lld]",
141 context.counter->classid, context.counter->iftype,
142 context.counter->iotype, context.counter->intend,
143 context.counter->ifname, context.bytes);
145 if (context.counter->intend == NFACCT_COUNTER ||
146 context.counter->intend == NFACCT_TETH_COUNTER) {
147 if (g_system->apps) {
148 stc_app_value_s *lookup_app;
149 stc_rstn_value_s *lookup_rstn;
151 stc_monitor_app_update_iface_counter(&context);
153 lookup_app = g_hash_table_lookup(g_system->apps,
154 GUINT_TO_POINTER(context.counter->classid));
156 stc_monitor_app_update_counter(lookup_app, &context);
158 lookup_rstn = g_hash_table_lookup(g_system->rstns,
159 GUINT_TO_POINTER(context.counter->classid));
161 int32_t limit_exceeded = 0;
162 g_slist_foreach(lookup_rstn->rules,
163 __check_rstn_limit_exceeded, &limit_exceeded);
165 if (limit_exceeded != 0)
170 if (g_system->rstns) {
171 stc_rstn_value_s *lookup_value;
172 uint32_t classid = context.counter->classid;
174 stc_monitor_rstn_update_iface_counter(&context);
175 context.counter->classid = classid;
177 lookup_value = g_hash_table_lookup(g_system->rstns,
178 GUINT_TO_POINTER(classid));
180 g_slist_foreach(lookup_value->rules,
181 stc_monitor_rstn_update_counter,
188 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
191 struct counter_arg *carg = user_data;
192 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
193 if (carg->initiate) {
195 * TODO: this will be used when daemon starts to update existing
196 * counter data if present.
198 populate_counters(cnt_name, carg);
201 long long int *bytes_p =
202 (long long int *)RTA_DATA(attr_list[NFACCT_BYTES]);
203 int bytes = be64toh(*bytes_p);
205 ++carg->serialized_counters;
206 __fill_nfacct_result(cnt_name, bytes, carg);
213 static int __post_fill_counters(void *user_data)
215 struct counter_arg *carg = user_data;
223 static void __process_network_counter(struct genl *ans,
224 struct counter_arg *carg)
226 struct netlink_serialization_params ser_params = {
229 .eval_attr = __fill_counters,
230 .post_eval_attr = __post_fill_counters,
233 netlink_serialization_command *netlink =
234 netlink_create_command(&ser_params);
236 STC_LOGE("Can not create command");
240 netlink->deserialize_answer(&(netlink->params));
243 static gboolean __process_contr_reply(GIOChannel *source,
244 GIOCondition condition,
247 int sock = g_io_channel_unix_get_fd(source);
250 stc_s *stc = stc_get_manager();
253 void __gcov_flush(void);
257 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
258 (condition & G_IO_NVAL)) {
259 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
261 STC_LOGE("Counter socket received G_IO event, closing socket."
262 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
263 (condition & G_IO_ERR), (condition & G_IO_HUP),
264 (condition & G_IO_NVAL));
265 __close_and_reopen_contr_sock(g_system);
269 ans = MALLOC0(struct genl, 1);
271 STC_LOGE("Failed allocate memory to genl reply message");
276 STC_LOGE("Can't get stc data");
280 ret = read_netlink(sock, ans, sizeof(struct genl));
285 stc->carg->ans_len = ret;
286 stc->carg->last_run_time = time(NULL);
288 __process_network_counter(ans, stc->carg);
290 g_idle_add(stc_monitor_app_flush_stats_to_db, NULL);
291 g_idle_add(stc_monitor_rstn_flush_contr_to_db, NULL);
298 static gboolean __update_contr_cb(void *user_data)
300 /* Here we just sent command, answer we receive in another callback */
301 stc_s *stc = stc_get_manager();
302 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
304 stc->carg = MALLOC0(counter_arg_s, 1);
305 if (stc->carg == NULL)
306 return TRUE; /* we need to continue the timer */
308 stc->carg->sock = g_system->contr_sock;
312 void __gcov_flush(void);
316 /* STC_LOGD("Get all counters"); */
317 nfacct_send_get_all(stc->carg);
319 /* we need to continue the timer */
323 static void __fill_exceptions_list(void)
325 stc_plugin_fill_exception_list();
328 stc_error_e stc_plugin_monitor_initialize(void)
330 __STC_LOG_FUNC_ENTER__;
332 stc_system_s *system = MALLOC0(stc_system_s, 1);
333 GIOChannel *gio = NULL;
335 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY,
336 "stc_system_s malloc fail!");
338 /* initializing current classid */
339 init_current_classid();
341 /* initializing cgroups */
344 /* creating monitored application tree */
345 system->apps = stc_monitor_apps_init();
346 system->rstns = stc_monitor_rstns_init();
348 /* create netlink socket for updating kernel counters */
349 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
350 if (system->contr_sock < 0) {
351 STC_LOGE("failed to open socket");
353 return STC_ERROR_FAIL;
356 gio = g_io_channel_unix_new(system->contr_sock);
357 system->contr_gsource_id =
358 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
359 (GIOFunc) __process_contr_reply,
361 g_io_channel_unref(gio);
365 stc_monitor_app_add_by_iface(STC_TOTAL_DATACALL);
366 stc_monitor_app_add_by_iface(STC_TOTAL_WIFI);
367 stc_monitor_app_add_by_iface(STC_TOTAL_BLUETOOTH);
368 stc_monitor_app_add_by_iface(STC_TOTAL_IPV4);
369 stc_monitor_app_add_by_iface(STC_TOTAL_IPV6);
371 __update_contr_cb(NULL);
373 /* registering periodic kernel counters update callback */
374 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
377 if (g_system->contr_timer_id == 0) {
378 STC_LOGE("Failed to register kernel counters update timer");
379 __close_contr_sock(g_system);
380 return STC_ERROR_FAIL;
383 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
384 (int *)&g_system->background_state);
386 __fill_exceptions_list();
387 stc_monitor_rstns_load();
389 __STC_LOG_FUNC_EXIT__;
390 return STC_ERROR_NONE;
393 stc_error_e stc_plugin_monitor_deinitialize(void)
395 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
397 /* close netlink socket for updating kernel counters */
398 __close_contr_sock(g_system);
400 /* remove kernel counters update timer */
401 if (g_system->contr_timer_id > 0) {
402 g_source_remove(g_system->contr_timer_id);
403 g_system->contr_timer_id = 0;
406 /* destroy monitored application tree */
407 g_hash_table_destroy(g_system->apps);
408 g_system->apps = NULL;
410 /* destroy restriction rules tree */
411 g_hash_table_destroy(g_system->rstns);
412 g_system->rstns = NULL;
416 return STC_ERROR_NONE;
419 GHashTable *stc_monitor_get_system_apps(void)
421 ret_value_msg_if(g_system == NULL, NULL,
422 "stc monitor not initialized!");
424 ret_value_msg_if(g_system->apps == NULL, NULL,
427 return g_system->apps;
430 GHashTable *stc_monitor_get_system_rstns(void)
432 ret_value_msg_if(g_system == NULL, NULL,
433 "stc monitor not initialized!");
435 ret_value_msg_if(g_system->rstns == NULL, NULL,
438 return g_system->rstns;
441 int stc_monitor_get_contr_sock(void)
443 ret_value_msg_if(g_system == NULL, 0,
444 "stc monitor not initialized!");
446 return g_system->contr_sock;
449 time_t stc_monitor_get_last_month_ts(void)
451 ret_value_msg_if(g_system == NULL, 0,
452 "stc monitor not initialized!");
454 return g_system->last_month_ts;
457 void stc_monitor_set_last_month_ts(time_t time)
459 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
461 g_system->last_month_ts = time;
464 time_t stc_monitor_get_last_week_ts(void)
466 ret_value_msg_if(g_system == NULL, 0,
467 "stc monitor not initialized!");
469 return g_system->last_week_ts;
472 void stc_monitor_set_last_week_ts(time_t time)
474 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
476 g_system->last_week_ts = time;
479 time_t stc_monitor_get_last_day_ts(void)
481 ret_value_msg_if(g_system == NULL, 0,
482 "stc monitor not initialized!");
484 return g_system->last_day_ts;
487 void stc_monitor_set_last_day_ts(time_t time)
489 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
491 g_system->last_day_ts = time;
494 void stc_monitor_set_rstns_updated(gboolean value)
496 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
498 g_system->rstns_updated = value;
501 gboolean stc_monitor_get_rstns_updated(void)
503 ret_value_msg_if(g_system == NULL, FALSE,
504 "stc monitor not initialized!");
506 return g_system->rstns_updated;
509 void stc_monitor_set_apps_updated(gboolean value)
511 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
513 g_system->apps_updated = value;
516 gboolean stc_monitor_get_apps_updated(void)
518 ret_value_msg_if(g_system == NULL, FALSE,
519 "stc monitor not initialized!");
521 return g_system->apps_updated;
524 void stc_monitor_set_background_state(gboolean value)
526 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
528 g_system->background_state = value;
531 gboolean stc_monitor_get_background_state(void)
533 ret_value_msg_if(g_system == NULL, FALSE,
534 "stc monitor not initialized!");
536 return g_system->background_state;
539 void stc_monitor_update_by_connection(void *data)
541 stc_connection_s *connection = (stc_connection_s *)data;
543 if (connection != NULL && connection->path != NULL) {
544 stc_monitor_app_remove_by_connection(connection);
545 stc_monitor_rstn_remove_by_connection(connection);
547 iptables_flush_chains();
549 stc_monitor_app_add_by_connection(connection);
550 stc_monitor_rstn_add_by_connection(connection);
554 void stc_monitor_add_by_connection(void *data)
556 stc_connection_s *connection = (stc_connection_s *)data;
558 if (connection != NULL && connection->path != NULL) {
559 stc_monitor_app_add_by_connection(connection);
560 stc_monitor_rstn_add_by_connection(connection);
564 void stc_monitor_remove_by_connection(void *data)
566 stc_connection_s *connection = (stc_connection_s *)data;
568 if (connection != NULL && connection->path != NULL) {
569 stc_monitor_app_remove_by_connection(connection);
570 stc_monitor_rstn_remove_by_connection(connection);
574 API stc_plugin_monitor_s stc_plugin_monitor = {
576 stc_plugin_monitor_initialize,
577 .deinitialize_plugin =
578 stc_plugin_monitor_deinitialize,
580 stc_plugin_monitor_app_add,
581 .remove_application =
582 stc_plugin_monitor_app_remove,
583 .lookup_application =
584 stc_plugin_monitor_app_lookup,
586 stc_plugin_monitor_rstn_add,
587 .remove_restriction =
588 stc_plugin_monitor_rstn_remove,
590 stc_plugin_monitor_connection_init,
592 stc_plugin_monitor_connection_deinit,
594 stc_plugin_monitor_proc_add,
596 stc_plugin_monitor_proc_remove,
598 stc_plugin_monitor_proc_move,
599 .update_process_ground =
600 stc_plugin_monitor_proc_update_ground