90507a60568708212dd6db4b0b47b7a06eaf8f7f
[vpp.git] / src / plugins / gbp / gbp_scanner.c
1 /*
2  * gbp.h : Group Based Policy
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <plugins/gbp/gbp_scanner.h>
19 #include <plugins/gbp/gbp_endpoint.h>
20 #include <plugins/gbp/gbp_vxlan.h>
21
22 /**
23  * Scanner logger
24  */
25 vlib_log_class_t gs_logger;
26
27 /**
28  * Scanner state
29  */
30 static bool gs_enabled;
31
32 #define GBP_SCANNER_DBG(...)                                      \
33     vlib_log_debug (gs_logger, __VA_ARGS__);
34
35 static uword
36 gbp_scanner (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
37 {
38   uword event_type, *event_data = 0;
39   bool do_scan = 0;
40
41   while (1)
42     {
43       do_scan = 0;
44
45       if (gs_enabled)
46         {
47           /* scan every 'inactive threshold' seconds */
48           vlib_process_wait_for_event_or_clock (vm,
49                                                 gbp_endpoint_scan_threshold
50                                                 ());
51         }
52       else
53         vlib_process_wait_for_event (vm);
54
55       event_type = vlib_process_get_events (vm, &event_data);
56       vec_reset_length (event_data);
57
58       switch (event_type)
59         {
60         case ~0:
61           /* timer expired */
62           do_scan = 1;
63           break;
64
65         case GBP_ENDPOINT_SCAN_START:
66           gs_enabled = 1;
67           break;
68
69         case GBP_ENDPOINT_SCAN_STOP:
70           gs_enabled = 0;
71           break;
72
73         case GBP_ENDPOINT_SCAN_SET_TIME:
74           break;
75
76         default:
77           ASSERT (0);
78         }
79
80       if (do_scan)
81         {
82           GBP_SCANNER_DBG ("start");
83           gbp_endpoint_scan (vm);
84           GBP_SCANNER_DBG ("stop");
85         }
86     }
87   return 0;
88 }
89
90 /* *INDENT-OFF* */
91 VLIB_REGISTER_NODE (gbp_scanner_node) = {
92     .function = gbp_scanner,
93     .type = VLIB_NODE_TYPE_PROCESS,
94     .name = "gbp-scanner",
95 };
96 /* *INDENT-ON* */
97
98 static clib_error_t *
99 gbp_scanner_cli (vlib_main_t * vm,
100                  unformat_input_t * input, vlib_cli_command_t * cmd)
101 {
102   vlib_cli_output (vm, "GBP-scanner: enabled:%d interval:%f",
103                    gs_enabled, gbp_endpoint_scan_threshold ());
104
105   return (NULL);
106 }
107
108 /*?
109  * Show GBP scanner
110  *
111  * @cliexpar
112  * @cliexstart{show gbp scanner}
113  * @cliexend
114  ?*/
115 /* *INDENT-OFF* */
116 VLIB_CLI_COMMAND (gbp_scanner_cli_node, static) = {
117   .path = "show gbp scanner",
118   .short_help = "show gbp scanner",
119   .function = gbp_scanner_cli,
120 };
121 /* *INDENT-ON* */
122
123 static clib_error_t *
124 gbp_scanner_init (vlib_main_t * vm)
125 {
126   gs_logger = vlib_log_register_class ("gbp", "scan");
127
128   return (NULL);
129 }
130
131 VLIB_INIT_FUNCTION (gbp_scanner_init);
132
133 /*
134  * fd.io coding-style-patch-verification: ON
135  *
136  * Local Variables:
137  * eval: (c-set-style "gnu")
138  * End:
139  */