Line data Source code
1 : /* 2 : * Copyright (c) 2020 Cisco and/or its affiliates. 3 : * Licensed under the Apache License, Version 2.0 (the "License"); 4 : * you may not use this file except in compliance with the License. 5 : * You may obtain a copy of the License at: 6 : * 7 : * http://www.apache.org/licenses/LICENSE-2.0 8 : * 9 : * Unless required by applicable law or agreed to in writing, software 10 : * distributed under the License is distributed on an "AS IS" BASIS, 11 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : * See the License for the specific language governing permissions and 13 : * limitations under the License. 14 : */ 15 : 16 : #include <cnat/cnat_session.h> 17 : #include <vlibmemory/api.h> 18 : #include <cnat/cnat_client.h> 19 : 20 : static uword 21 575 : cnat_scanner_process (vlib_main_t * vm, 22 : vlib_node_runtime_t * rt, vlib_frame_t * f) 23 : { 24 575 : uword event_type, *event_data = 0; 25 575 : cnat_main_t *cm = &cnat_main; 26 : f64 start_time; 27 575 : int enabled = 0, i = 0; 28 : 29 : while (1) 30 : { 31 621 : if (enabled) 32 41 : vlib_process_wait_for_event_or_clock (vm, cm->scanner_timeout); 33 : else 34 580 : vlib_process_wait_for_event (vm); 35 : 36 46 : event_type = vlib_process_get_events (vm, &event_data); 37 46 : vec_reset_length (event_data); 38 : 39 46 : start_time = vlib_time_now (vm); 40 : 41 46 : switch (event_type) 42 : { 43 : /* timer expired */ 44 39 : case ~0: 45 39 : break; 46 5 : case CNAT_SCANNER_OFF: 47 5 : enabled = 0; 48 5 : break; 49 2 : case CNAT_SCANNER_ON: 50 2 : enabled = 1; 51 2 : break; 52 0 : default: 53 0 : ASSERT (0); 54 : } 55 : 56 46 : cnat_client_throttle_pool_process (); 57 46 : i = cnat_session_scan (vm, start_time, i); 58 : } 59 : return 0; 60 : } 61 : 62 164204 : VLIB_REGISTER_NODE (cnat_scanner_process_node) = { 63 : .function = cnat_scanner_process, 64 : .type = VLIB_NODE_TYPE_PROCESS, 65 : .name = "cnat-scanner-process", 66 : }; 67 : 68 : static clib_error_t * 69 4 : cnat_scanner_cmd (vlib_main_t * vm, 70 : unformat_input_t * input, vlib_cli_command_t * c) 71 : { 72 : cnat_scanner_cmd_t cmd; 73 : 74 4 : cmd = CNAT_SCANNER_ON; 75 : 76 8 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 77 : { 78 4 : if (unformat (input, "on")) 79 2 : cmd = CNAT_SCANNER_ON; 80 2 : else if (unformat (input, "off")) 81 2 : cmd = CNAT_SCANNER_OFF; 82 : else 83 0 : return (clib_error_return (0, "unknown input '%U'", 84 : format_unformat_error, input)); 85 : } 86 4 : cnat_enable_disable_scanner (cmd); 87 : 88 4 : return (NULL); 89 : } 90 : 91 257065 : VLIB_CLI_COMMAND (cnat_scanner_cmd_node, static) = { 92 : .path = "test cnat scanner", 93 : .function = cnat_scanner_cmd, 94 : .short_help = "test cnat scanner", 95 : }; 96 : 97 : static clib_error_t * 98 575 : cnat_scanner_init (vlib_main_t * vm) 99 : { 100 575 : cnat_main_t *cm = &cnat_main; 101 575 : cm->scanner_node_index = cnat_scanner_process_node.index; 102 : 103 575 : return (NULL); 104 : } 105 : 106 1727 : VLIB_INIT_FUNCTION (cnat_scanner_init); 107 : 108 : /* 109 : * fd.io coding-style-patch-verification: ON 110 : * 111 : * Local Variables: 112 : * eval: (c-set-style "gnu") 113 : * End: 114 : */