Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #include <vnet/session/application.h> |
| 16 | #include <vnet/session/session.h> |
| 17 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 18 | u8 * |
| 19 | format_stream_session_fifos (u8 * s, va_list * args) |
| 20 | { |
| 21 | stream_session_t *ss = va_arg (*args, stream_session_t *); |
| 22 | s = format (s, " Rx fifo: %U", format_svm_fifo, ss->server_rx_fifo, 1); |
| 23 | s = format (s, " Tx fifo: %U", format_svm_fifo, ss->server_tx_fifo, 1); |
| 24 | return s; |
| 25 | } |
| 26 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | /** |
| 28 | * Format stream session as per the following format |
| 29 | * |
| 30 | * verbose: |
| 31 | * "Connection", "Rx fifo", "Tx fifo", "Session Index" |
| 32 | * non-verbose: |
| 33 | * "Connection" |
| 34 | */ |
| 35 | u8 * |
| 36 | format_stream_session (u8 * s, va_list * args) |
| 37 | { |
| 38 | stream_session_t *ss = va_arg (*args, stream_session_t *); |
| 39 | int verbose = va_arg (*args, int); |
| 40 | transport_proto_vft_t *tp_vft; |
| 41 | u8 *str = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 42 | tp_vft = session_get_transport_vft (ss->session_type); |
| 43 | |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 44 | if (verbose == 1) |
| 45 | str = format (0, "%-10u%-10u%-10lld", |
| 46 | svm_fifo_max_dequeue (ss->server_rx_fifo), |
| 47 | svm_fifo_max_enqueue (ss->server_tx_fifo), |
| 48 | stream_session_get_index (ss)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 49 | |
| 50 | if (ss->session_state == SESSION_STATE_READY) |
| 51 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 52 | s = format (s, "%U", tp_vft->format_connection, ss->connection_index, |
| 53 | ss->thread_index, verbose); |
| 54 | if (verbose == 1) |
| 55 | s = format (s, "%v", str); |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 56 | if (verbose > 1) |
| 57 | s = format (s, "%U", format_stream_session_fifos, ss); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 58 | } |
| 59 | else if (ss->session_state == SESSION_STATE_LISTENING) |
| 60 | { |
| 61 | s = format (s, "%-40U%v", tp_vft->format_listener, ss->connection_index, |
| 62 | str); |
| 63 | } |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 64 | else if (ss->session_state == SESSION_STATE_CONNECTING) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 65 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 66 | s = format (s, "%-40U%v", tp_vft->format_half_open, |
| 67 | ss->connection_index, str); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 68 | } |
| 69 | else if (ss->session_state == SESSION_STATE_CLOSED) |
| 70 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 71 | s = format (s, "[CL] %-40U", tp_vft->format_connection, |
| 72 | ss->connection_index, ss->thread_index, verbose); |
| 73 | if (verbose == 1) |
| 74 | s = format (s, "%v", str); |
| 75 | if (verbose > 1) |
| 76 | s = format (s, "%U", format_stream_session_fifos, ss); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 80 | clib_warning ("Session in state: %d!", ss->session_state); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | vec_free (str); |
| 84 | |
| 85 | return s; |
| 86 | } |
| 87 | |
| 88 | static clib_error_t * |
| 89 | show_session_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 90 | vlib_cli_command_t * cmd) |
| 91 | { |
| 92 | session_manager_main_t *smm = &session_manager_main; |
| 93 | int verbose = 0, i; |
| 94 | stream_session_t *pool; |
| 95 | stream_session_t *s; |
| 96 | u8 *str = 0; |
| 97 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 98 | if (!smm->is_enabled) |
| 99 | { |
flyingeagle23 | e212506 | 2017-04-20 20:01:14 +0800 | [diff] [blame] | 100 | return clib_error_return (0, "session layer is not enabled"); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 103 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 104 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 105 | if (unformat (input, "verbose %d", &verbose)) |
| 106 | ; |
| 107 | else if (unformat (input, "verbose")) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 108 | verbose = 1; |
| 109 | else |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | for (i = 0; i < vec_len (smm->sessions); i++) |
| 114 | { |
| 115 | u32 once_per_pool; |
| 116 | pool = smm->sessions[i]; |
| 117 | |
| 118 | once_per_pool = 1; |
| 119 | |
| 120 | if (pool_elts (pool)) |
| 121 | { |
| 122 | |
| 123 | vlib_cli_output (vm, "Thread %d: %d active sessions", |
| 124 | i, pool_elts (pool)); |
| 125 | if (verbose) |
| 126 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 127 | if (once_per_pool && verbose == 1) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 128 | { |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 129 | str = |
| 130 | format (str, "%-50s%-15s%-10s%-10s%-10s", "Connection", |
| 131 | "State", "Rx-f", "Tx-f", "S-idx"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 132 | vlib_cli_output (vm, "%v", str); |
| 133 | vec_reset_length (str); |
| 134 | once_per_pool = 0; |
| 135 | } |
| 136 | |
| 137 | /* *INDENT-OFF* */ |
| 138 | pool_foreach (s, pool, |
| 139 | ({ |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 140 | vec_reset_length (str); |
| 141 | str = format (str, "%U", format_stream_session, s, verbose); |
Florin Coras | bb292f4 | 2017-05-19 09:49:19 -0700 | [diff] [blame] | 142 | vlib_cli_output (vm, "%v", str); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 143 | })); |
| 144 | /* *INDENT-ON* */ |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | vlib_cli_output (vm, "Thread %d: no active sessions", i); |
| 149 | } |
| 150 | vec_free (str); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 155 | /* *INDENT-OFF* */ |
| 156 | VLIB_CLI_COMMAND (show_session_command, static) = |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 157 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 158 | .path = "show session", |
| 159 | .short_help = "show session [verbose]", |
| 160 | .function = show_session_command_fn, |
| 161 | }; |
| 162 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 163 | |
| 164 | static clib_error_t * |
| 165 | clear_session_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 166 | vlib_cli_command_t * cmd) |
| 167 | { |
| 168 | session_manager_main_t *smm = &session_manager_main; |
| 169 | u32 thread_index = 0; |
| 170 | u32 session_index = ~0; |
| 171 | stream_session_t *pool, *session; |
| 172 | application_t *server; |
| 173 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 174 | if (!smm->is_enabled) |
| 175 | { |
flyingeagle23 | e212506 | 2017-04-20 20:01:14 +0800 | [diff] [blame] | 176 | return clib_error_return (0, "session layer is not enabled"); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 179 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 180 | { |
| 181 | if (unformat (input, "thread %d", &thread_index)) |
| 182 | ; |
| 183 | else if (unformat (input, "session %d", &session_index)) |
| 184 | ; |
| 185 | else |
| 186 | return clib_error_return (0, "unknown input `%U'", |
| 187 | format_unformat_error, input); |
| 188 | } |
| 189 | |
| 190 | if (session_index == ~0) |
| 191 | return clib_error_return (0, "session <nn> required, but not set."); |
| 192 | |
| 193 | if (thread_index > vec_len (smm->sessions)) |
| 194 | return clib_error_return (0, "thread %d out of range [0-%d]", |
| 195 | thread_index, vec_len (smm->sessions)); |
| 196 | |
| 197 | pool = smm->sessions[thread_index]; |
| 198 | |
| 199 | if (pool_is_free_index (pool, session_index)) |
| 200 | return clib_error_return (0, "session %d not active", session_index); |
| 201 | |
| 202 | session = pool_elt_at_index (pool, session_index); |
| 203 | server = application_get (session->app_index); |
| 204 | |
| 205 | /* Disconnect both app and transport */ |
| 206 | server->cb_fns.session_disconnect_callback (session); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 211 | /* *INDENT-OFF* */ |
| 212 | VLIB_CLI_COMMAND (clear_session_command, static) = |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 213 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 214 | .path = "clear session", |
| 215 | .short_help = "clear session thread <thread> session <index>", |
| 216 | .function = clear_session_command_fn, |
| 217 | }; |
| 218 | /* *INDENT-ON* */ |
| 219 | |
| 220 | static clib_error_t * |
| 221 | session_enable_disable_fn (vlib_main_t * vm, unformat_input_t * input, |
| 222 | vlib_cli_command_t * cmd) |
| 223 | { |
| 224 | u8 is_en = 1; |
| 225 | |
| 226 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 227 | { |
| 228 | if (unformat (input, "enable")) |
| 229 | is_en = 1; |
| 230 | else if (unformat (input, "disable")) |
| 231 | is_en = 0; |
| 232 | else |
| 233 | return clib_error_return (0, "unknown input `%U'", |
| 234 | format_unformat_error, input); |
| 235 | } |
| 236 | |
| 237 | return vnet_session_enable_disable (vm, is_en); |
| 238 | } |
| 239 | |
| 240 | /* *INDENT-OFF* */ |
| 241 | VLIB_CLI_COMMAND (session_enable_disable_command, static) = |
| 242 | { |
| 243 | .path = "session", |
| 244 | .short_help = "session [enable|disable]", |
| 245 | .function = session_enable_disable_fn, |
| 246 | }; |
| 247 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * fd.io coding-style-patch-verification: ON |
| 251 | * |
| 252 | * Local Variables: |
| 253 | * eval: (c-set-style "gnu") |
| 254 | * End: |
| 255 | */ |