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;
35 static stc_manager_stop_cb g_stop_cb = NULL;
37 static int __vconf_get_int(const char *key, int *value)
41 ret = vconf_get_int(key, value);
42 if (ret != VCONF_OK) {
43 STC_LOGE("Failed to get vconfkey [%s] value", key);
50 static stc_error_e __close_contr_sock(stc_system_s *system)
52 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
54 /* close netlink socket for updating kernel counters */
55 if (system->contr_sock != -1) {
56 close(system->contr_sock);
57 system->contr_sock = -1;
60 if (system->contr_gsource_id != 0) {
61 g_source_remove(system->contr_gsource_id);
62 system->contr_gsource_id = 0;
65 return STC_ERROR_NONE;
68 static gboolean __process_contr_reply(GIOChannel *source,
69 GIOCondition condition,
72 static void __free_system(stc_system_s *system)
74 /* destroy monitored application tree */
76 g_hash_table_destroy(system->apps);
77 /* destroy restriction rules tree */
79 g_hash_table_destroy(system->rstns);
83 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
85 GIOChannel *gio = NULL;
86 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
88 /* close netlink socket for updating kernel counters */
89 if (system->contr_sock != -1) {
90 close(system->contr_sock);
91 system->contr_sock = -1;
94 if (system->contr_gsource_id != 0) {
95 g_source_remove(system->contr_gsource_id);
96 system->contr_gsource_id = 0;
99 /* create netlink socket for updating kernel counters */
100 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
101 if (system->contr_sock < 0) {
102 STC_LOGE("failed to open socket");
103 __free_system(system);
106 return STC_ERROR_FAIL;
109 gio = g_io_channel_unix_new(system->contr_sock);
110 system->contr_gsource_id =
111 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
112 (GIOFunc) __process_contr_reply,
114 g_io_channel_unref(gio);
116 return STC_ERROR_NONE;
119 static void __check_rstn_limit_exceeded(gpointer data,
122 stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
123 int32_t *limit_exceeded = (int32_t *)user_data;
125 if (rstn_data->limit_exceeded != 0)
126 *limit_exceeded = rstn_data->limit_exceeded;
129 static void __fill_nfacct_result(char *cnt_name, long long int bytes,
130 struct counter_arg *carg)
132 stc_monitor_rstn_reset_time_counters_if_required();
134 struct nfacct_rule counter = {
141 classid_bytes_context_s context = {
144 .data_limit_exceeded = FALSE,
147 if (!recreate_counter_by_name(cnt_name, &counter)) {
148 STC_LOGE("Can't parse counter name %s", cnt_name);
153 STC_LOGI("classid[\033[1;36m%u\033[0;m] iftype[%u] "
154 "iotype[%d] intend[%d] ifname[%s] bytes[%lld]",
155 context.counter->classid, context.counter->iftype,
156 context.counter->iotype, context.counter->intend,
157 context.counter->ifname, context.bytes);
159 if (context.counter->intend == NFACCT_COUNTER ||
160 context.counter->intend == NFACCT_TETH_COUNTER) {
161 if (g_system->apps) {
162 stc_app_value_s *lookup_app;
163 stc_rstn_value_s *lookup_rstn;
165 stc_monitor_app_update_iface_counter(&context);
167 lookup_app = g_hash_table_lookup(g_system->apps,
168 GUINT_TO_POINTER(context.counter->classid));
170 stc_monitor_app_update_counter(lookup_app, &context);
172 lookup_rstn = g_hash_table_lookup(g_system->rstns,
173 GUINT_TO_POINTER(context.counter->classid));
175 int32_t limit_exceeded = 0;
176 g_slist_foreach(lookup_rstn->rules,
177 __check_rstn_limit_exceeded, &limit_exceeded);
179 if (limit_exceeded != 0)
184 if (g_system->rstns) {
185 stc_rstn_value_s *lookup_value;
186 uint32_t classid = context.counter->classid;
188 stc_monitor_rstn_update_iface_counter(&context);
189 context.counter->classid = classid;
191 lookup_value = g_hash_table_lookup(g_system->rstns,
192 GUINT_TO_POINTER(classid));
194 g_slist_foreach(lookup_value->rules,
195 stc_monitor_rstn_update_counter,
202 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
205 struct counter_arg *carg = user_data;
206 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
207 if (carg->initiate) {
209 * TODO: this will be used when daemon starts to update existing
210 * counter data if present.
212 populate_counters(cnt_name, carg);
215 long long int *bytes_p =
216 (long long int *)RTA_DATA(attr_list[NFACCT_BYTES]);
217 int bytes = be64toh(*bytes_p);
219 ++carg->serialized_counters;
220 __fill_nfacct_result(cnt_name, bytes, carg);
227 static int __post_fill_counters(void *user_data)
229 struct counter_arg *carg = user_data;
237 static void __process_network_counter(struct genl *ans,
238 struct counter_arg *carg)
240 struct netlink_serialization_params ser_params = {
243 .eval_attr = __fill_counters,
244 .post_eval_attr = __post_fill_counters,
247 netlink_serialization_command *netlink =
248 netlink_create_command(&ser_params);
250 STC_LOGE("Can not create command");
254 netlink->deserialize_answer(&(netlink->params));
257 static gboolean __process_contr_reply(GIOChannel *source,
258 GIOCondition condition,
261 int sock = g_io_channel_unix_get_fd(source);
264 stc_s *stc = stc_get_manager();
267 void __gcov_flush(void);
271 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
272 (condition & G_IO_NVAL)) {
273 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
275 STC_LOGE("Counter socket received G_IO event, closing socket."
276 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
277 (condition & G_IO_ERR), (condition & G_IO_HUP),
278 (condition & G_IO_NVAL));
279 __close_and_reopen_contr_sock(g_system);
283 ans = MALLOC0(struct genl, 1);
285 STC_LOGE("Failed allocate memory to genl reply message");
290 STC_LOGE("Can't get stc data");
294 ret = read_netlink(sock, ans, sizeof(struct genl));
299 stc->carg->ans_len = ret;
300 stc->carg->last_run_time = time(NULL);
302 __process_network_counter(ans, stc->carg);
304 g_idle_add(stc_monitor_app_flush_stats_to_db, NULL);
305 g_idle_add(stc_monitor_rstn_flush_contr_to_db, NULL);
312 static gboolean __update_contr_cb(void *user_data)
314 /* Here we just sent command, answer we receive in another callback */
315 stc_s *stc = stc_get_manager();
316 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
318 stc->carg = MALLOC0(counter_arg_s, 1);
319 if (stc->carg == NULL)
320 return TRUE; /* we need to continue the timer */
322 stc->carg->sock = g_system->contr_sock;
326 void __gcov_flush(void);
330 /* STC_LOGD("Get all counters"); */
331 nfacct_send_get_all(stc->carg);
333 /* we need to continue the timer */
337 static void __fill_exceptions_list(void)
339 stc_plugin_fill_exception_list();
342 stc_error_e stc_plugin_monitor_initialize(stc_manager_stop_cb stop_cb)
344 __STC_LOG_FUNC_ENTER__;
346 stc_system_s *system = MALLOC0(stc_system_s, 1);
347 GIOChannel *gio = NULL;
349 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY,
350 "stc_system_s malloc fail!");
352 ret_value_msg_if(stop_cb == NULL, STC_ERROR_INVALID_PARAMETER,
353 "stop_cb callback is NULL");
357 /* initializing current classid */
358 init_current_classid();
360 /* initializing cgroups */
363 /* creating monitored application tree */
364 system->apps = stc_monitor_apps_init();
365 system->rstns = stc_monitor_rstns_init();
367 /* create netlink socket for updating kernel counters */
368 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
369 if (system->contr_sock < 0) {
370 STC_LOGE("failed to open socket");
371 __free_system(system);
373 return STC_ERROR_FAIL;
376 gio = g_io_channel_unix_new(system->contr_sock);
377 system->contr_gsource_id =
378 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
379 (GIOFunc) __process_contr_reply,
381 g_io_channel_unref(gio);
385 stc_monitor_app_add_by_iface(STC_TOTAL_DATACALL);
386 stc_monitor_app_add_by_iface(STC_TOTAL_WIFI);
387 stc_monitor_app_add_by_iface(STC_TOTAL_BLUETOOTH);
388 stc_monitor_app_add_by_iface(STC_TOTAL_IPV4);
389 stc_monitor_app_add_by_iface(STC_TOTAL_IPV6);
391 __update_contr_cb(NULL);
393 /* registering periodic kernel counters update callback */
394 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
397 if (g_system->contr_timer_id == 0) {
398 STC_LOGE("Failed to register kernel counters update timer");
399 __close_contr_sock(g_system);
400 return STC_ERROR_FAIL;
403 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
404 (int *)&g_system->background_state);
406 __fill_exceptions_list();
407 stc_monitor_rstns_load();
409 __STC_LOG_FUNC_EXIT__;
410 return STC_ERROR_NONE;
413 stc_error_e stc_plugin_monitor_deinitialize(void)
415 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
417 /* close netlink socket for updating kernel counters */
418 __close_contr_sock(g_system);
420 /* remove kernel counters update timer */
421 if (g_system->contr_timer_id > 0) {
422 g_source_remove(g_system->contr_timer_id);
423 g_system->contr_timer_id = 0;
426 __free_system(g_system);
430 return STC_ERROR_NONE;
433 GHashTable *stc_monitor_get_system_apps(void)
435 ret_value_msg_if(g_system == NULL, NULL,
436 "stc monitor not initialized!");
438 ret_value_msg_if(g_system->apps == NULL, NULL,
441 return g_system->apps;
444 GHashTable *stc_monitor_get_system_rstns(void)
446 ret_value_msg_if(g_system == NULL, NULL,
447 "stc monitor not initialized!");
449 ret_value_msg_if(g_system->rstns == NULL, NULL,
452 return g_system->rstns;
455 int stc_monitor_get_contr_sock(void)
457 ret_value_msg_if(g_system == NULL, 0,
458 "stc monitor not initialized!");
460 return g_system->contr_sock;
463 time_t stc_monitor_get_last_month_ts(void)
465 ret_value_msg_if(g_system == NULL, 0,
466 "stc monitor not initialized!");
468 return g_system->last_month_ts;
471 void stc_monitor_set_last_month_ts(time_t time)
473 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
475 g_system->last_month_ts = time;
478 time_t stc_monitor_get_last_week_ts(void)
480 ret_value_msg_if(g_system == NULL, 0,
481 "stc monitor not initialized!");
483 return g_system->last_week_ts;
486 void stc_monitor_set_last_week_ts(time_t time)
488 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
490 g_system->last_week_ts = time;
493 time_t stc_monitor_get_last_day_ts(void)
495 ret_value_msg_if(g_system == NULL, 0,
496 "stc monitor not initialized!");
498 return g_system->last_day_ts;
501 void stc_monitor_set_last_day_ts(time_t time)
503 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
505 g_system->last_day_ts = time;
508 void stc_monitor_set_rstns_updated(gboolean value)
510 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
512 g_system->rstns_updated = value;
515 gboolean stc_monitor_get_rstns_updated(void)
517 ret_value_msg_if(g_system == NULL, FALSE,
518 "stc monitor not initialized!");
520 return g_system->rstns_updated;
523 void stc_monitor_set_apps_updated(gboolean value)
525 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
527 g_system->apps_updated = value;
530 gboolean stc_monitor_get_apps_updated(void)
532 ret_value_msg_if(g_system == NULL, FALSE,
533 "stc monitor not initialized!");
535 return g_system->apps_updated;
538 void stc_monitor_set_background_state(gboolean value)
540 ret_msg_if(g_system == NULL, "stc monitor not initialized!");
542 g_system->background_state = value;
545 gboolean stc_monitor_get_background_state(void)
547 ret_value_msg_if(g_system == NULL, FALSE,
548 "stc monitor not initialized!");
550 return g_system->background_state;
553 void stc_monitor_update_by_connection(void *data)
555 stc_connection_s *connection = (stc_connection_s *)data;
557 if (connection != NULL && connection->path != NULL) {
558 iptables_flush_chains();
560 stc_monitor_app_add_by_connection(connection);
561 stc_monitor_rstn_add_by_connection(connection);
565 void stc_monitor_add_by_connection(void *data)
567 stc_connection_s *connection = (stc_connection_s *)data;
569 if (connection != NULL && connection->path != NULL) {
570 stc_monitor_app_add_by_connection(connection);
571 stc_monitor_rstn_add_by_connection(connection);
575 void stc_monitor_remove_by_connection(void *data)
577 stc_connection_s *connection = (stc_connection_s *)data;
579 if (connection != NULL && connection->path != NULL) {
580 stc_monitor_app_remove_by_connection(connection);
581 stc_monitor_rstn_remove_by_connection(connection);
585 API stc_plugin_monitor_s stc_plugin_monitor = {
587 stc_plugin_monitor_initialize,
588 .deinitialize_plugin =
589 stc_plugin_monitor_deinitialize,
591 stc_plugin_monitor_app_add,
592 .remove_application =
593 stc_plugin_monitor_app_remove,
594 .lookup_application =
595 stc_plugin_monitor_app_lookup,
597 stc_plugin_monitor_rstn_add,
598 .remove_restriction =
599 stc_plugin_monitor_rstn_remove,
601 stc_plugin_monitor_connection_init,
603 stc_plugin_monitor_connection_deinit,
605 stc_plugin_monitor_proc_add,
607 stc_plugin_monitor_proc_remove,
609 stc_plugin_monitor_proc_move,
610 .update_process_ground =
611 stc_plugin_monitor_proc_update_ground