Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2015-2019 Cisco and/or its affiliates. |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 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 <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vnet/session/application.h> |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 19 | #include <vnet/session/application_interface.h> |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 20 | #include <vnet/session/application_local.h> |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 21 | #include <vnet/session/session_rules_table.h> |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 22 | #include <vnet/session/session_table.h> |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 23 | #include <vnet/session/session.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 24 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 25 | #include <vnet/ip/ip_types_api.h> |
| 26 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | #include <vnet/vnet_msg_enum.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 28 | |
| 29 | #define vl_typedefs /* define message structures */ |
| 30 | #include <vnet/vnet_all_api_h.h> |
| 31 | #undef vl_typedefs |
| 32 | |
| 33 | #define vl_endianfun /* define message structures */ |
| 34 | #include <vnet/vnet_all_api_h.h> |
| 35 | #undef vl_endianfun |
| 36 | |
| 37 | /* instantiate all the print functions we know about */ |
| 38 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 39 | #define vl_printfun |
| 40 | #include <vnet/vnet_all_api_h.h> |
| 41 | #undef vl_printfun |
| 42 | |
| 43 | #include <vlibapi/api_helper_macros.h> |
| 44 | |
| 45 | #define foreach_session_api_msg \ |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 46 | _(APP_ATTACH, app_attach) \ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 47 | _(APPLICATION_DETACH, application_detach) \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 48 | _(SESSION_ENABLE_DISABLE, session_enable_disable) \ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 49 | _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \ |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 50 | _(SESSION_RULE_ADD_DEL, session_rule_add_del) \ |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 51 | _(SESSION_RULES_DUMP, session_rules_dump) \ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 52 | _(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \ |
| 53 | _(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \ |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 54 | _(APP_ADD_CERT_KEY_PAIR, app_add_cert_key_pair) \ |
| 55 | _(APP_DEL_CERT_KEY_PAIR, app_del_cert_key_pair) \ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 56 | _(APP_WORKER_ADD_DEL, app_worker_add_del) \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 57 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 58 | static transport_proto_t |
| 59 | api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp) |
| 60 | { |
| 61 | switch (*api_tp) |
| 62 | { |
| 63 | case TRANSPORT_PROTO_API_TCP: |
| 64 | return TRANSPORT_PROTO_TCP; |
| 65 | case TRANSPORT_PROTO_API_UDP: |
| 66 | return TRANSPORT_PROTO_UDP; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 67 | case TRANSPORT_PROTO_API_TLS: |
| 68 | return TRANSPORT_PROTO_TLS; |
| 69 | case TRANSPORT_PROTO_API_UDPC: |
| 70 | return TRANSPORT_PROTO_UDPC; |
| 71 | case TRANSPORT_PROTO_API_QUIC: |
| 72 | return TRANSPORT_PROTO_QUIC; |
| 73 | default: |
| 74 | return TRANSPORT_PROTO_NONE; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static vl_api_transport_proto_t |
| 79 | api_session_transport_proto_encode (const transport_proto_t tp) |
| 80 | { |
| 81 | switch (tp) |
| 82 | { |
| 83 | case TRANSPORT_PROTO_TCP: |
| 84 | return TRANSPORT_PROTO_API_TCP; |
| 85 | case TRANSPORT_PROTO_UDP: |
| 86 | return TRANSPORT_PROTO_API_UDP; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 87 | case TRANSPORT_PROTO_TLS: |
| 88 | return TRANSPORT_PROTO_API_TLS; |
| 89 | case TRANSPORT_PROTO_UDPC: |
| 90 | return TRANSPORT_PROTO_API_UDPC; |
| 91 | case TRANSPORT_PROTO_QUIC: |
| 92 | return TRANSPORT_PROTO_API_QUIC; |
| 93 | default: |
| 94 | return TRANSPORT_PROTO_API_NONE; |
| 95 | } |
| 96 | } |
| 97 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | static int |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 99 | session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 100 | { |
| 101 | clib_error_t *error; |
| 102 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 103 | return SESSION_E_BAPI_NO_FD; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 104 | error = vl_api_send_fd_msg (reg, fds, n_fds); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 105 | if (error) |
| 106 | { |
| 107 | clib_error_report (error); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 108 | return SESSION_E_BAPI_SEND_FD; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 113 | static int |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 114 | mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg) |
| 115 | { |
| 116 | int rv; |
| 117 | u8 try = 0; |
| 118 | while (try < 100) |
| 119 | { |
| 120 | rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, |
| 121 | SESSION_MQ_CTRL_EVT_RING, |
| 122 | SVM_Q_NOWAIT, msg); |
| 123 | if (!rv) |
| 124 | return 0; |
| 125 | try++; |
Florin Coras | e2ea193 | 2018-12-17 23:08:14 -0800 | [diff] [blame] | 126 | usleep (1); |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 127 | } |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 128 | clib_warning ("failed to alloc msg"); |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 133 | mq_send_session_accepted_cb (session_t * s) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 134 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 135 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 136 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 137 | svm_msg_q_t *vpp_queue, *app_mq; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 138 | session_t *listener; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 139 | session_accepted_msg_t *mp; |
| 140 | session_event_t *evt; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 141 | application_t *app; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 142 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 143 | app = application_get (app_wrk->app_index); |
| 144 | app_mq = app_wrk->event_queue; |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 145 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 146 | return SESSION_E_MQ_MSG_ALLOC; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 147 | |
| 148 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 149 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 150 | evt->event_type = SESSION_CTRL_EVT_ACCEPTED; |
| 151 | mp = (session_accepted_msg_t *) evt->data; |
Florin Coras | 87c6570 | 2019-01-26 14:33:26 -0800 | [diff] [blame] | 152 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 153 | mp->context = app->app_index; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 154 | mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); |
| 155 | mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 156 | mp->segment_handle = session_segment_handle (s); |
Nathan Skrzypczak | c00f480 | 2019-12-03 16:25:11 +0100 | [diff] [blame] | 157 | mp->flags = s->flags; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 158 | |
| 159 | if (session_has_transport (s)) |
| 160 | { |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 161 | listener = listen_session_get_from_handle (s->listener_handle); |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 162 | mp->listener_handle = app_listen_session_handle (listener); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 163 | if (application_is_proxy (app)) |
| 164 | { |
| 165 | listener = |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 166 | app_worker_first_listener (app_wrk, session_get_fib_proto (s), |
| 167 | session_get_transport_proto (s)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 168 | if (listener) |
| 169 | mp->listener_handle = listen_session_get_handle (listener); |
| 170 | } |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 171 | vpp_queue = session_main_get_vpp_event_queue (s->thread_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 172 | mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); |
| 173 | mp->handle = session_handle (s); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 174 | |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 175 | session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ ); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 179 | ct_connection_t *ct; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 180 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 181 | ct = (ct_connection_t *) session_get_transport (s); |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 182 | listener = listen_session_get_from_handle (s->listener_handle); |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 183 | mp->listener_handle = app_listen_session_handle (listener); |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 184 | mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type); |
| 185 | mp->rmt.port = ct->c_rmt_port; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 186 | mp->handle = session_handle (s); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 187 | vpp_queue = session_main_get_vpp_event_queue (0); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 188 | mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 189 | } |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 190 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 195 | static inline void |
| 196 | mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh, |
| 197 | session_evt_type_t evt_type) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 198 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 199 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 200 | session_disconnected_msg_t *mp; |
| 201 | svm_msg_q_t *app_mq; |
| 202 | session_event_t *evt; |
| 203 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 204 | app_mq = app_wrk->event_queue; |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 205 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 206 | return; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 207 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 208 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 209 | evt->event_type = evt_type; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 210 | mp = (session_disconnected_msg_t *) evt->data; |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 211 | mp->handle = sh; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 212 | mp->context = app_wrk->api_client_index; |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 213 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 216 | static inline void |
| 217 | mq_notify_close_subscribers (u32 app_index, session_handle_t sh, |
| 218 | svm_fifo_t * f, session_evt_type_t evt_type) |
| 219 | { |
| 220 | app_worker_t *app_wrk; |
| 221 | application_t *app; |
| 222 | int i; |
| 223 | |
| 224 | app = application_get (app_index); |
| 225 | if (!app) |
| 226 | return; |
| 227 | |
| 228 | for (i = 0; i < f->n_subscribers; i++) |
| 229 | { |
| 230 | if (!(app_wrk = application_get_worker (app, f->subscribers[i]))) |
| 231 | continue; |
| 232 | mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 237 | mq_send_session_disconnected_cb (session_t * s) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 238 | { |
| 239 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
| 240 | session_handle_t sh = session_handle (s); |
| 241 | |
| 242 | mq_send_session_close_evt (app_wrk, session_handle (s), |
| 243 | SESSION_CTRL_EVT_DISCONNECTED); |
| 244 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 245 | if (svm_fifo_n_subscribers (s->rx_fifo)) |
| 246 | mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo, |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 247 | SESSION_CTRL_EVT_DISCONNECTED); |
| 248 | } |
| 249 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 250 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 251 | mq_send_session_reset_cb (session_t * s) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 252 | { |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 253 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
| 254 | session_handle_t sh = session_handle (s); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 255 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 256 | mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET); |
| 257 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 258 | if (svm_fifo_n_subscribers (s->rx_fifo)) |
| 259 | mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo, |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 260 | SESSION_CTRL_EVT_RESET); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 263 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 264 | mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context, |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 265 | session_t * s, session_error_t err) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 266 | { |
| 267 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 268 | session_connected_msg_t *mp; |
| 269 | svm_msg_q_t *vpp_mq, *app_mq; |
| 270 | transport_connection_t *tc; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 271 | app_worker_t *app_wrk; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 272 | session_event_t *evt; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 273 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 274 | app_wrk = app_worker_get (app_wrk_index); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 275 | app_mq = app_wrk->event_queue; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 276 | if (!app_mq) |
| 277 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 278 | clib_warning ("app %u with api index: %u not attached", |
| 279 | app_wrk->app_index, app_wrk->api_client_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 283 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 284 | return SESSION_E_MQ_MSG_ALLOC; |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 285 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 286 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 287 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 288 | evt->event_type = SESSION_CTRL_EVT_CONNECTED; |
| 289 | mp = (session_connected_msg_t *) evt->data; |
Florin Coras | 87c6570 | 2019-01-26 14:33:26 -0800 | [diff] [blame] | 290 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 291 | mp->context = api_context; |
| 292 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 293 | if (err) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 294 | goto done; |
| 295 | |
| 296 | if (session_has_transport (s)) |
| 297 | { |
| 298 | tc = session_get_transport (s); |
| 299 | if (!tc) |
| 300 | { |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 301 | clib_warning ("failed to retrieve transport!"); |
| 302 | err = SESSION_E_REFUSED; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 303 | goto done; |
| 304 | } |
| 305 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 306 | vpp_mq = session_main_get_vpp_event_queue (s->thread_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 307 | mp->handle = session_handle (s); |
| 308 | mp->vpp_event_queue_address = pointer_to_uword (vpp_mq); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 309 | |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 310 | session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ ); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 311 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 312 | mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); |
| 313 | mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 314 | mp->segment_handle = session_segment_handle (s); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 318 | ct_connection_t *cct; |
| 319 | session_t *ss; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 320 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 321 | cct = (ct_connection_t *) session_get_transport (s); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 322 | mp->handle = session_handle (s); |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 323 | mp->lcl.port = cct->c_lcl_port; |
| 324 | mp->lcl.is_ip4 = cct->c_is_ip4; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 325 | vpp_mq = session_main_get_vpp_event_queue (0); |
Florin Coras | 54693d2 | 2018-07-17 10:46:29 -0700 | [diff] [blame] | 326 | mp->vpp_event_queue_address = pointer_to_uword (vpp_mq); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 327 | mp->server_rx_fifo = pointer_to_uword (s->rx_fifo); |
| 328 | mp->server_tx_fifo = pointer_to_uword (s->tx_fifo); |
| 329 | mp->segment_handle = session_segment_handle (s); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 330 | ss = ct_session_get_peer (s); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 331 | mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo); |
| 332 | mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo); |
| 333 | mp->ct_segment_handle = session_segment_handle (ss); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | done: |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 337 | mp->retval = err; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 338 | |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 339 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 343 | int |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 344 | mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context, |
| 345 | session_handle_t handle, int rv) |
| 346 | { |
| 347 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 348 | svm_msg_q_t *app_mq, *vpp_evt_q; |
Yu Ping | 0b81915 | 2019-05-07 02:24:30 +0800 | [diff] [blame] | 349 | transport_endpoint_t tep; |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 350 | session_bound_msg_t *mp; |
| 351 | app_worker_t *app_wrk; |
| 352 | session_event_t *evt; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 353 | app_listener_t *al; |
| 354 | session_t *ls = 0; |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 355 | app_wrk = app_worker_get (app_wrk_index); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 356 | app_mq = app_wrk->event_queue; |
| 357 | if (!app_mq) |
| 358 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 359 | clib_warning ("app %u with api index: %u not attached", |
| 360 | app_wrk->app_index, app_wrk->api_client_index); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 361 | return -1; |
| 362 | } |
| 363 | |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 364 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 365 | return SESSION_E_MQ_MSG_ALLOC; |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 366 | |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 367 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 368 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 369 | evt->event_type = SESSION_CTRL_EVT_BOUND; |
| 370 | mp = (session_bound_msg_t *) evt->data; |
| 371 | mp->context = api_context; |
| 372 | |
| 373 | if (rv) |
| 374 | goto done; |
| 375 | |
| 376 | mp->handle = handle; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 377 | al = app_listener_get_w_handle (handle); |
| 378 | if (al->session_index != SESSION_INVALID_INDEX) |
| 379 | ls = app_listener_get_session (al); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 380 | else |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 381 | ls = app_listener_get_local_session (al); |
Yu Ping | 0b81915 | 2019-05-07 02:24:30 +0800 | [diff] [blame] | 382 | |
| 383 | session_get_endpoint (ls, &tep, 1 /* is_lcl */ ); |
| 384 | mp->lcl_port = tep.port; |
| 385 | mp->lcl_is_ip4 = tep.is_ip4; |
| 386 | clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip)); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 387 | |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 388 | vpp_evt_q = session_main_get_vpp_event_queue (0); |
Florin Coras | 5f45e01 | 2019-01-23 09:21:30 -0800 | [diff] [blame] | 389 | mp->vpp_evt_q = pointer_to_uword (vpp_evt_q); |
| 390 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 391 | if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL) |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 392 | { |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 393 | mp->rx_fifo = pointer_to_uword (ls->rx_fifo); |
| 394 | mp->tx_fifo = pointer_to_uword (ls->tx_fifo); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | done: |
| 398 | mp->retval = rv; |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 399 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 403 | void |
| 404 | mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh, |
| 405 | u32 context, int rv) |
| 406 | { |
| 407 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 408 | session_unlisten_reply_msg_t *ump; |
| 409 | svm_msg_q_t *app_mq; |
| 410 | session_event_t *evt; |
| 411 | |
| 412 | app_mq = app_wrk->event_queue; |
| 413 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 414 | return; |
| 415 | |
| 416 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 417 | clib_memset (evt, 0, sizeof (*evt)); |
| 418 | evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY; |
| 419 | ump = (session_unlisten_reply_msg_t *) evt->data; |
| 420 | ump->context = context; |
| 421 | ump->handle = sh; |
| 422 | ump->retval = rv; |
| 423 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 424 | } |
| 425 | |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 426 | static void |
| 427 | mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh) |
| 428 | { |
Florin Coras | 68b7e58 | 2020-01-21 18:33:23 -0800 | [diff] [blame] | 429 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 430 | session_migrated_msg_t *mp; |
| 431 | svm_msg_q_t *vpp_evt_q; |
| 432 | app_worker_t *app_wrk; |
| 433 | session_event_t *evt; |
| 434 | svm_msg_q_t *app_mq; |
| 435 | |
| 436 | app_wrk = app_worker_get (s->app_wrk_index); |
| 437 | app_mq = app_wrk->event_queue; |
| 438 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 439 | return; |
| 440 | |
| 441 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 442 | clib_memset (evt, 0, sizeof (*evt)); |
| 443 | evt->event_type = SESSION_CTRL_EVT_MIGRATED; |
| 444 | mp = (session_migrated_msg_t *) evt->data; |
| 445 | mp->handle = session_handle (s); |
| 446 | mp->new_handle = new_sh; |
| 447 | mp->vpp_thread_index = session_thread_from_handle (new_sh); |
| 448 | vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index); |
| 449 | mp->vpp_evt_q = pointer_to_uword (vpp_evt_q); |
| 450 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 453 | static int |
| 454 | mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle) |
| 455 | { |
| 456 | int fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 457 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 458 | session_app_add_segment_msg_t *mp; |
| 459 | vl_api_registration_t *reg; |
| 460 | app_worker_t *app_wrk; |
| 461 | session_event_t *evt; |
| 462 | svm_msg_q_t *app_mq; |
| 463 | fifo_segment_t *fs; |
| 464 | ssvm_private_t *sp; |
| 465 | u8 fd_flags = 0; |
| 466 | |
| 467 | app_wrk = app_worker_get (app_wrk_index); |
| 468 | |
| 469 | reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index); |
| 470 | if (!reg) |
| 471 | { |
| 472 | clib_warning ("no api registration for client: %u", |
| 473 | app_wrk->api_client_index); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | fs = segment_manager_get_segment_w_handle (segment_handle); |
| 478 | sp = &fs->ssvm; |
| 479 | if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD) |
| 480 | { |
| 481 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
| 482 | { |
| 483 | clib_warning ("can't send memfd fd"); |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 488 | fds[n_fds] = sp->fd; |
| 489 | n_fds += 1; |
| 490 | } |
| 491 | |
| 492 | app_mq = app_wrk->event_queue; |
| 493 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 494 | return -1; |
| 495 | |
| 496 | if (n_fds) |
| 497 | session_send_fds (reg, fds, n_fds); |
| 498 | |
| 499 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 500 | clib_memset (evt, 0, sizeof (*evt)); |
| 501 | evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT; |
| 502 | mp = (session_app_add_segment_msg_t *) evt->data; |
| 503 | clib_memset (mp, 0, sizeof (*mp)); |
| 504 | mp->segment_size = sp->ssvm_size; |
| 505 | mp->fd_flags = fd_flags; |
| 506 | mp->segment_handle = segment_handle; |
| 507 | strncpy ((char *) mp->segment_name, (char *) sp->name, |
| 508 | sizeof (mp->segment_name) - 1); |
| 509 | |
| 510 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int |
| 516 | mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle) |
| 517 | { |
| 518 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 519 | session_app_del_segment_msg_t *mp; |
| 520 | vl_api_registration_t *reg; |
| 521 | app_worker_t *app_wrk; |
| 522 | session_event_t *evt; |
| 523 | svm_msg_q_t *app_mq; |
| 524 | |
| 525 | app_wrk = app_worker_get (app_wrk_index); |
| 526 | reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index); |
| 527 | if (!reg) |
| 528 | { |
| 529 | clib_warning ("no registration: %u", app_wrk->api_client_index); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | app_mq = app_wrk->event_queue; |
| 534 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 535 | return -1; |
| 536 | |
| 537 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 538 | clib_memset (evt, 0, sizeof (*evt)); |
| 539 | evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT; |
| 540 | mp = (session_app_del_segment_msg_t *) evt->data; |
| 541 | clib_memset (mp, 0, sizeof (*mp)); |
| 542 | mp->segment_handle = segment_handle; |
| 543 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Florin Coras | 9ace36d | 2019-10-28 13:14:17 -0700 | [diff] [blame] | 548 | static void |
| 549 | mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf) |
| 550 | { |
| 551 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 552 | session_cleanup_msg_t *mp; |
| 553 | svm_msg_q_t *app_mq; |
| 554 | session_event_t *evt; |
| 555 | app_worker_t *app_wrk; |
| 556 | |
| 557 | /* Only propagate session cleanup notification */ |
| 558 | if (ntf == SESSION_CLEANUP_TRANSPORT) |
| 559 | return; |
| 560 | |
| 561 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 562 | if (!app_wrk) |
| 563 | return; |
| 564 | |
| 565 | app_mq = app_wrk->event_queue; |
| 566 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 567 | return; |
| 568 | |
| 569 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 570 | clib_memset (evt, 0, sizeof (*evt)); |
| 571 | evt->event_type = SESSION_CTRL_EVT_CLEANUP; |
| 572 | mp = (session_cleanup_msg_t *) evt->data; |
| 573 | mp->handle = session_handle (s); |
| 574 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 575 | } |
| 576 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 577 | static session_cb_vft_t session_mq_cb_vft = { |
| 578 | .session_accept_callback = mq_send_session_accepted_cb, |
| 579 | .session_disconnect_callback = mq_send_session_disconnected_cb, |
| 580 | .session_connected_callback = mq_send_session_connected_cb, |
| 581 | .session_reset_callback = mq_send_session_reset_cb, |
| 582 | .session_migrate_callback = mq_send_session_migrate_cb, |
Florin Coras | 9ace36d | 2019-10-28 13:14:17 -0700 | [diff] [blame] | 583 | .session_cleanup_callback = mq_send_session_cleanup_cb, |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 584 | .add_segment_callback = mq_send_add_segment_cb, |
| 585 | .del_segment_callback = mq_send_del_segment_cb, |
| 586 | }; |
| 587 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 588 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 589 | vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) |
| 590 | { |
| 591 | vl_api_session_enable_disable_reply_t *rmp; |
| 592 | vlib_main_t *vm = vlib_get_main (); |
| 593 | int rv = 0; |
| 594 | |
| 595 | vnet_session_enable_disable (vm, mp->is_enable); |
| 596 | REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY); |
| 597 | } |
| 598 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 599 | static void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 600 | vl_api_app_attach_t_handler (vl_api_app_attach_t * mp) |
| 601 | { |
| 602 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 603 | vl_api_app_attach_reply_t *rmp; |
| 604 | ssvm_private_t *segp, *evt_q_segment; |
| 605 | vnet_app_attach_args_t _a, *a = &_a; |
| 606 | u8 fd_flags = 0, ctrl_thread; |
| 607 | vl_api_registration_t *reg; |
| 608 | svm_msg_q_t *ctrl_mq; |
| 609 | |
| 610 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 611 | if (!reg) |
| 612 | return; |
| 613 | |
| 614 | if (session_main_is_enabled () == 0) |
| 615 | { |
| 616 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 617 | goto done; |
| 618 | } |
| 619 | |
| 620 | STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <= |
| 621 | sizeof (mp->options), |
| 622 | "Out of options, fix api message definition"); |
| 623 | |
| 624 | clib_memset (a, 0, sizeof (*a)); |
| 625 | a->api_client_index = mp->client_index; |
| 626 | a->options = mp->options; |
| 627 | a->session_cb_vft = &session_mq_cb_vft; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 628 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 629 | a->namespace_id = vl_api_from_api_to_new_vec (&mp->namespace_id); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 630 | |
| 631 | if ((rv = vnet_application_attach (a))) |
| 632 | { |
| 633 | clib_warning ("attach returned: %d", rv); |
| 634 | vec_free (a->namespace_id); |
| 635 | goto done; |
| 636 | } |
| 637 | vec_free (a->namespace_id); |
| 638 | |
| 639 | /* Send event queues segment */ |
| 640 | if ((evt_q_segment = session_main_get_evt_q_segment ())) |
| 641 | { |
| 642 | fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT; |
| 643 | fds[n_fds] = evt_q_segment->fd; |
| 644 | n_fds += 1; |
| 645 | } |
| 646 | /* Send fifo segment fd if needed */ |
| 647 | if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) |
| 648 | { |
| 649 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 650 | fds[n_fds] = a->segment->fd; |
| 651 | n_fds += 1; |
| 652 | } |
| 653 | if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 654 | { |
| 655 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
| 656 | fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q); |
| 657 | n_fds += 1; |
| 658 | } |
| 659 | |
| 660 | done: |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 661 | /* *INDENT-OFF* */ |
| 662 | REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({ |
| 663 | if (!rv) |
| 664 | { |
Vratko Polak | 4f59985 | 2019-11-08 10:32:39 +0100 | [diff] [blame] | 665 | ctrl_thread = vlib_num_workers () ? 1 : 0; |
Nathan Skrzypczak | 9d3e1b4 | 2019-11-07 10:29:24 +0100 | [diff] [blame] | 666 | ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 667 | segp = a->segment; |
| 668 | rmp->app_index = clib_host_to_net_u32 (a->app_index); |
| 669 | rmp->app_mq = pointer_to_uword (a->app_evt_q); |
| 670 | rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq); |
| 671 | rmp->vpp_ctrl_mq_thread = ctrl_thread; |
| 672 | rmp->n_fds = n_fds; |
| 673 | rmp->fd_flags = fd_flags; |
| 674 | if (vec_len (segp->name)) |
| 675 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 676 | vl_api_vec_to_api_string (segp->name, &rmp->segment_name); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 677 | } |
| 678 | rmp->segment_size = segp->ssvm_size; |
| 679 | rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle); |
| 680 | } |
| 681 | })); |
| 682 | /* *INDENT-ON* */ |
| 683 | |
| 684 | if (n_fds) |
| 685 | session_send_fds (reg, fds, n_fds); |
| 686 | } |
| 687 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 688 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 689 | vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp) |
| 690 | { |
| 691 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 692 | vl_api_app_worker_add_del_reply_t *rmp; |
| 693 | vl_api_registration_t *reg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 694 | application_t *app; |
| 695 | u8 fd_flags = 0; |
| 696 | |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 697 | if (session_main_is_enabled () == 0) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 698 | { |
| 699 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 700 | goto done; |
| 701 | } |
| 702 | |
| 703 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 704 | if (!reg) |
| 705 | return; |
| 706 | |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 707 | app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index)); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 708 | if (!app) |
| 709 | { |
| 710 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 711 | goto done; |
| 712 | } |
| 713 | |
| 714 | vnet_app_worker_add_del_args_t args = { |
| 715 | .app_index = app->app_index, |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 716 | .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index), |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 717 | .api_client_index = mp->client_index, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 718 | .is_add = mp->is_add |
| 719 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 720 | rv = vnet_app_worker_add_del (&args); |
| 721 | if (rv) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 722 | { |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 723 | clib_warning ("app worker add/del returned: %d", rv); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 724 | goto done; |
| 725 | } |
| 726 | |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 727 | if (!mp->is_add) |
| 728 | goto done; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 729 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 730 | /* Send fifo segment fd if needed */ |
| 731 | if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) |
| 732 | { |
| 733 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 734 | fds[n_fds] = args.segment->fd; |
| 735 | n_fds += 1; |
| 736 | } |
| 737 | if (application_segment_manager_properties (app)->use_mq_eventfd) |
| 738 | { |
| 739 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
| 740 | fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q); |
| 741 | n_fds += 1; |
| 742 | } |
| 743 | |
| 744 | /* *INDENT-OFF* */ |
| 745 | done: |
| 746 | REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({ |
| 747 | rmp->is_add = mp->is_add; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 748 | rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 749 | rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle); |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 750 | if (!rv && mp->is_add) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 751 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 752 | if (vec_len (args.segment->name)) |
| 753 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 754 | vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 755 | } |
| 756 | rmp->app_event_queue_address = pointer_to_uword (args.evt_q); |
| 757 | rmp->n_fds = n_fds; |
| 758 | rmp->fd_flags = fd_flags; |
| 759 | } |
| 760 | })); |
| 761 | /* *INDENT-ON* */ |
| 762 | |
| 763 | if (n_fds) |
| 764 | session_send_fds (reg, fds, n_fds); |
| 765 | } |
| 766 | |
| 767 | static void |
Florin Coras | 888d9f0 | 2020-04-02 23:00:13 +0000 | [diff] [blame] | 768 | vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) |
| 769 | { |
| 770 | vl_api_application_detach_reply_t *rmp; |
| 771 | int rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 772 | vnet_app_detach_args_t _a, *a = &_a; |
| 773 | application_t *app; |
| 774 | |
| 775 | if (session_main_is_enabled () == 0) |
| 776 | { |
| 777 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 778 | goto done; |
| 779 | } |
| 780 | |
| 781 | app = application_lookup (mp->client_index); |
| 782 | if (app) |
| 783 | { |
| 784 | a->app_index = app->app_index; |
| 785 | a->api_client_index = mp->client_index; |
| 786 | rv = vnet_application_detach (a); |
| 787 | } |
| 788 | |
| 789 | done: |
| 790 | REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); |
| 791 | } |
| 792 | |
| 793 | static void |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 794 | vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) |
| 795 | { |
| 796 | vl_api_app_namespace_add_del_reply_t *rmp; |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 797 | u32 appns_index = 0; |
| 798 | u8 *ns_id = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 799 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 800 | if (session_main_is_enabled () == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 801 | { |
| 802 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 803 | goto done; |
| 804 | } |
| 805 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 806 | ns_id = vl_api_from_api_to_new_vec (&mp->namespace_id); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 807 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 808 | vnet_app_namespace_add_del_args_t args = { |
| 809 | .ns_id = ns_id, |
Florin Coras | 9a9adb2 | 2017-10-26 08:16:59 -0700 | [diff] [blame] | 810 | .secret = clib_net_to_host_u64 (mp->secret), |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 811 | .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index), |
| 812 | .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id), |
| 813 | .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id), |
| 814 | .is_add = 1 |
| 815 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 816 | rv = vnet_app_namespace_add_del (&args); |
| 817 | if (!rv) |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 818 | { |
| 819 | appns_index = app_namespace_index_from_id (ns_id); |
| 820 | if (appns_index == APP_NAMESPACE_INVALID_INDEX) |
| 821 | { |
| 822 | clib_warning ("app ns lookup failed"); |
| 823 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 824 | } |
| 825 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 826 | vec_free (ns_id); |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 827 | |
| 828 | /* *INDENT-OFF* */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 829 | done: |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 830 | REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({ |
| 831 | if (!rv) |
| 832 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
| 833 | })); |
| 834 | /* *INDENT-ON* */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 837 | static void |
| 838 | vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp) |
| 839 | { |
| 840 | vl_api_session_rule_add_del_reply_t *rmp; |
| 841 | session_rule_add_del_args_t args; |
| 842 | session_rule_table_add_del_args_t *table_args = &args.table_args; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 843 | int rv = 0; |
| 844 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 845 | clib_memset (&args, 0, sizeof (args)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 846 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 847 | ip_prefix_decode (&mp->lcl, &table_args->lcl); |
| 848 | ip_prefix_decode (&mp->rmt, &table_args->rmt); |
| 849 | |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 850 | table_args->lcl_port = mp->lcl_port; |
| 851 | table_args->rmt_port = mp->rmt_port; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 852 | table_args->action_index = clib_net_to_host_u32 (mp->action_index); |
| 853 | table_args->is_add = mp->is_add; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 854 | mp->tag[sizeof (mp->tag) - 1] = 0; |
| 855 | table_args->tag = format (0, "%s", mp->tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 856 | args.appns_index = clib_net_to_host_u32 (mp->appns_index); |
| 857 | args.scope = mp->scope; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 858 | args.transport_proto = |
| 859 | api_session_transport_proto_decode (&mp->transport_proto) == |
| 860 | TRANSPORT_PROTO_UDP ? 1 : 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 861 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 862 | rv = vnet_session_rule_add_del (&args); |
| 863 | if (rv) |
| 864 | clib_warning ("rule add del returned: %d", rv); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 865 | vec_free (table_args->tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 866 | REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY); |
| 867 | } |
| 868 | |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 869 | static void |
| 870 | send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 871 | u8 transport_proto, u32 appns_index, u8 * tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 872 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 873 | { |
| 874 | vl_api_session_rules_details_t *rmp = 0; |
| 875 | session_mask_or_match_4_t *match = |
| 876 | (session_mask_or_match_4_t *) & rule->match; |
| 877 | session_mask_or_match_4_t *mask = |
| 878 | (session_mask_or_match_4_t *) & rule->mask; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 879 | fib_prefix_t lcl, rmt; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 880 | |
| 881 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 882 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 883 | rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); |
| 884 | rmp->context = context; |
| 885 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 886 | clib_memset (&lcl, 0, sizeof (lcl)); |
| 887 | clib_memset (&rmt, 0, sizeof (rmt)); |
| 888 | ip_set (&lcl.fp_addr, &match->lcl_ip, 1); |
| 889 | ip_set (&rmt.fp_addr, &match->rmt_ip, 1); |
| 890 | lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip); |
| 891 | rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip); |
| 892 | |
| 893 | ip_prefix_encode (&lcl, &rmp->lcl); |
| 894 | ip_prefix_encode (&rmt, &rmp->rmt); |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 895 | rmp->lcl_port = match->lcl_port; |
| 896 | rmp->rmt_port = match->rmt_port; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 897 | rmp->action_index = clib_host_to_net_u32 (rule->action_index); |
| 898 | rmp->scope = |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 899 | is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL; |
| 900 | rmp->transport_proto = api_session_transport_proto_encode (transport_proto); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 901 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 902 | if (tag) |
| 903 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 904 | clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 905 | rmp->tag[vec_len (tag)] = 0; |
| 906 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 907 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 908 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | static void |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 912 | send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local, |
| 913 | u8 transport_proto, u32 appns_index, u8 * tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 914 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 915 | { |
| 916 | vl_api_session_rules_details_t *rmp = 0; |
| 917 | session_mask_or_match_6_t *match = |
| 918 | (session_mask_or_match_6_t *) & rule->match; |
| 919 | session_mask_or_match_6_t *mask = |
| 920 | (session_mask_or_match_6_t *) & rule->mask; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 921 | fib_prefix_t lcl, rmt; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 922 | |
| 923 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 924 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 925 | rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); |
| 926 | rmp->context = context; |
| 927 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 928 | clib_memset (&lcl, 0, sizeof (lcl)); |
| 929 | clib_memset (&rmt, 0, sizeof (rmt)); |
| 930 | ip_set (&lcl.fp_addr, &match->lcl_ip, 0); |
| 931 | ip_set (&rmt.fp_addr, &match->rmt_ip, 0); |
| 932 | lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip); |
| 933 | rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip); |
| 934 | |
| 935 | ip_prefix_encode (&lcl, &rmp->lcl); |
| 936 | ip_prefix_encode (&rmt, &rmp->rmt); |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 937 | rmp->lcl_port = match->lcl_port; |
| 938 | rmp->rmt_port = match->rmt_port; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 939 | rmp->action_index = clib_host_to_net_u32 (rule->action_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 940 | rmp->scope = |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 941 | is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL; |
| 942 | rmp->transport_proto = api_session_transport_proto_encode (transport_proto); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 943 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 944 | if (tag) |
| 945 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 946 | clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 947 | rmp->tag[vec_len (tag)] = 0; |
| 948 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 949 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 950 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static void |
| 954 | send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 955 | u8 tp, u8 is_local, u32 appns_index, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 956 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 957 | { |
| 958 | mma_rule_16_t *rule16; |
| 959 | mma_rule_40_t *rule40; |
| 960 | mma_rules_table_16_t *srt16; |
| 961 | mma_rules_table_40_t *srt40; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 962 | u32 ri; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 963 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 964 | if (is_local || fib_proto == FIB_PROTOCOL_IP4) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 965 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 966 | u8 *tag = 0; |
| 967 | /* *INDENT-OFF* */ |
| 968 | srt16 = &srt->session_rules_tables_16; |
| 969 | pool_foreach (rule16, srt16->rules, ({ |
| 970 | ri = mma_rules_table_rule_index_16 (srt16, rule16); |
| 971 | tag = session_rules_table_rule_tag (srt, ri, 1); |
| 972 | send_session_rule_details4 (rule16, is_local, tp, appns_index, tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 973 | reg, context); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 974 | })); |
| 975 | /* *INDENT-ON* */ |
| 976 | } |
| 977 | if (is_local || fib_proto == FIB_PROTOCOL_IP6) |
| 978 | { |
| 979 | u8 *tag = 0; |
| 980 | /* *INDENT-OFF* */ |
| 981 | srt40 = &srt->session_rules_tables_40; |
| 982 | pool_foreach (rule40, srt40->rules, ({ |
| 983 | ri = mma_rules_table_rule_index_40 (srt40, rule40); |
| 984 | tag = session_rules_table_rule_tag (srt, ri, 1); |
| 985 | send_session_rule_details6 (rule40, is_local, tp, appns_index, tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 986 | reg, context); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 987 | })); |
| 988 | /* *INDENT-ON* */ |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
| 992 | static void |
| 993 | vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp) |
| 994 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 995 | vl_api_registration_t *reg; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 996 | session_table_t *st; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 997 | u8 tp; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 998 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 999 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 1000 | if (!reg) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1001 | return; |
| 1002 | |
| 1003 | /* *INDENT-OFF* */ |
| 1004 | session_table_foreach (st, ({ |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 1005 | for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1006 | { |
| 1007 | send_session_rules_table_details (&st->session_rules[tp], |
| 1008 | st->active_fib_proto, tp, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1009 | st->is_local, st->appns_index, reg, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1010 | mp->context); |
| 1011 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1012 | })); |
| 1013 | /* *INDENT-ON* */ |
| 1014 | } |
| 1015 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1016 | static void |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1017 | vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1018 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1019 | vl_api_app_add_cert_key_pair_reply_t *rmp; |
| 1020 | vnet_app_add_cert_key_pair_args_t _a, *a = &_a; |
| 1021 | u32 certkey_len, key_len, cert_len; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1022 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1023 | if (session_main_is_enabled () == 0) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1024 | { |
| 1025 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1026 | goto done; |
| 1027 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1028 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1029 | cert_len = clib_net_to_host_u16 (mp->cert_len); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 1030 | if (cert_len > 10000) |
| 1031 | { |
| 1032 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1033 | goto done; |
| 1034 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1035 | |
| 1036 | certkey_len = clib_net_to_host_u16 (mp->certkey_len); |
| 1037 | if (certkey_len < cert_len) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1038 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1039 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1040 | goto done; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1041 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1042 | |
| 1043 | key_len = certkey_len - cert_len; |
| 1044 | if (key_len > 10000) |
| 1045 | { |
| 1046 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1047 | goto done; |
| 1048 | } |
| 1049 | |
| 1050 | clib_memset (a, 0, sizeof (*a)); |
| 1051 | vec_validate (a->cert, cert_len); |
| 1052 | vec_validate (a->key, key_len); |
| 1053 | clib_memcpy_fast (a->cert, mp->certkey, cert_len); |
| 1054 | clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len); |
| 1055 | rv = vnet_app_add_cert_key_pair (a); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1056 | vec_free (a->cert); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1057 | vec_free (a->key); |
| 1058 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1059 | done: |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1060 | /* *INDENT-OFF* */ |
| 1061 | REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({ |
| 1062 | if (!rv) |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1063 | rmp->index = clib_host_to_net_u32 (a->index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1064 | })); |
| 1065 | /* *INDENT-ON* */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static void |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1069 | vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1070 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1071 | vl_api_app_del_cert_key_pair_reply_t *rmp; |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1072 | u32 ckpair_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1073 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1074 | if (session_main_is_enabled () == 0) |
| 1075 | { |
| 1076 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1077 | goto done; |
| 1078 | } |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1079 | ckpair_index = clib_net_to_host_u32 (mp->index); |
| 1080 | rv = vnet_app_del_cert_key_pair (ckpair_index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1081 | |
| 1082 | done: |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1083 | REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* ### WILL BE DEPRECATED POST 20.01 ### */ |
| 1087 | static void |
| 1088 | vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t * |
| 1089 | mp) |
| 1090 | { |
| 1091 | vl_api_application_tls_cert_add_reply_t *rmp; |
| 1092 | app_cert_key_pair_t *ckpair; |
| 1093 | application_t *app; |
| 1094 | u32 cert_len; |
| 1095 | int rv = 0; |
| 1096 | if (session_main_is_enabled () == 0) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1097 | { |
| 1098 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1099 | goto done; |
| 1100 | } |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 1101 | if (!(app = application_lookup (mp->client_index))) |
| 1102 | { |
| 1103 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 1104 | goto done; |
| 1105 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1106 | cert_len = clib_net_to_host_u16 (mp->cert_len); |
| 1107 | if (cert_len > 10000) |
| 1108 | { |
| 1109 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1110 | goto done; |
| 1111 | } |
| 1112 | ckpair = app_cert_key_pair_get_default (); |
| 1113 | vec_validate (ckpair->cert, cert_len); |
| 1114 | clib_memcpy_fast (ckpair->cert, mp->cert, cert_len); |
| 1115 | |
| 1116 | done: |
| 1117 | REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY); |
| 1118 | } |
| 1119 | |
| 1120 | /* ### WILL BE DEPRECATED POST 20.01 ### */ |
| 1121 | static void |
| 1122 | vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t * |
| 1123 | mp) |
| 1124 | { |
| 1125 | vl_api_application_tls_key_add_reply_t *rmp; |
| 1126 | app_cert_key_pair_t *ckpair; |
| 1127 | application_t *app; |
| 1128 | u32 key_len; |
| 1129 | int rv = 0; |
| 1130 | if (session_main_is_enabled () == 0) |
| 1131 | { |
| 1132 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1133 | goto done; |
| 1134 | } |
| 1135 | if (!(app = application_lookup (mp->client_index))) |
| 1136 | { |
| 1137 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 1138 | goto done; |
| 1139 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1140 | key_len = clib_net_to_host_u16 (mp->key_len); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 1141 | if (key_len > 10000) |
| 1142 | { |
| 1143 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1144 | goto done; |
| 1145 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1146 | ckpair = app_cert_key_pair_get_default (); |
| 1147 | vec_validate (ckpair->key, key_len); |
| 1148 | clib_memcpy_fast (ckpair->key, mp->key, key_len); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1149 | done: |
| 1150 | REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY); |
| 1151 | } |
| 1152 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1153 | static clib_error_t * |
| 1154 | application_reaper_cb (u32 client_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1155 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1156 | application_t *app = application_lookup (client_index); |
| 1157 | vnet_app_detach_args_t _a, *a = &_a; |
| 1158 | if (app) |
| 1159 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1160 | a->app_index = app->app_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1161 | a->api_client_index = client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1162 | vnet_application_detach (a); |
| 1163 | } |
| 1164 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1165 | } |
| 1166 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1167 | VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1168 | |
| 1169 | #define vl_msg_name_crc_list |
| 1170 | #include <vnet/vnet_all_api_h.h> |
| 1171 | #undef vl_msg_name_crc_list |
| 1172 | |
| 1173 | static void |
| 1174 | setup_message_id_table (api_main_t * am) |
| 1175 | { |
| 1176 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1177 | foreach_vl_msg_name_crc_session; |
| 1178 | #undef _ |
| 1179 | } |
| 1180 | |
| 1181 | /* |
| 1182 | * session_api_hookup |
| 1183 | * Add uri's API message handlers to the table. |
Jim Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 1184 | * vlib has already mapped shared memory and |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1185 | * added the client registration handlers. |
| 1186 | * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() |
| 1187 | */ |
| 1188 | static clib_error_t * |
| 1189 | session_api_hookup (vlib_main_t * vm) |
| 1190 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1191 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1192 | |
| 1193 | #define _(N,n) \ |
| 1194 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1195 | vl_api_##n##_t_handler, \ |
| 1196 | vl_noop_handler, \ |
| 1197 | vl_api_##n##_t_endian, \ |
| 1198 | vl_api_##n##_t_print, \ |
| 1199 | sizeof(vl_api_##n##_t), 1); |
| 1200 | foreach_session_api_msg; |
| 1201 | #undef _ |
| 1202 | |
| 1203 | /* |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1204 | * Set up the (msg_name, crc, message-id) table |
| 1205 | */ |
| 1206 | setup_message_id_table (am); |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | VLIB_API_INIT_FUNCTION (session_api_hookup); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1212 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1213 | /* |
| 1214 | * fd.io coding-style-patch-verification: ON |
| 1215 | * |
| 1216 | * Local Variables: |
| 1217 | * eval: (c-set-style "gnu") |
| 1218 | * End: |
| 1219 | */ |