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) \ |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 52 | _(APP_ADD_CERT_KEY_PAIR, app_add_cert_key_pair) \ |
| 53 | _(APP_DEL_CERT_KEY_PAIR, app_del_cert_key_pair) \ |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 54 | _(APP_WORKER_ADD_DEL, app_worker_add_del) \ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 55 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 56 | static transport_proto_t |
| 57 | api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp) |
| 58 | { |
| 59 | switch (*api_tp) |
| 60 | { |
| 61 | case TRANSPORT_PROTO_API_TCP: |
| 62 | return TRANSPORT_PROTO_TCP; |
| 63 | case TRANSPORT_PROTO_API_UDP: |
| 64 | return TRANSPORT_PROTO_UDP; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 65 | case TRANSPORT_PROTO_API_TLS: |
| 66 | return TRANSPORT_PROTO_TLS; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 67 | case TRANSPORT_PROTO_API_QUIC: |
| 68 | return TRANSPORT_PROTO_QUIC; |
| 69 | default: |
| 70 | return TRANSPORT_PROTO_NONE; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static vl_api_transport_proto_t |
| 75 | api_session_transport_proto_encode (const transport_proto_t tp) |
| 76 | { |
| 77 | switch (tp) |
| 78 | { |
| 79 | case TRANSPORT_PROTO_TCP: |
| 80 | return TRANSPORT_PROTO_API_TCP; |
| 81 | case TRANSPORT_PROTO_UDP: |
| 82 | return TRANSPORT_PROTO_API_UDP; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 83 | case TRANSPORT_PROTO_TLS: |
| 84 | return TRANSPORT_PROTO_API_TLS; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 85 | case TRANSPORT_PROTO_QUIC: |
| 86 | return TRANSPORT_PROTO_API_QUIC; |
| 87 | default: |
| 88 | return TRANSPORT_PROTO_API_NONE; |
| 89 | } |
| 90 | } |
| 91 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 92 | static int |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 93 | 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] | 94 | { |
| 95 | clib_error_t *error; |
| 96 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 97 | return SESSION_E_BAPI_NO_FD; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 98 | error = vl_api_send_fd_msg (reg, fds, n_fds); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 99 | if (error) |
| 100 | { |
| 101 | clib_error_report (error); |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 102 | return SESSION_E_BAPI_SEND_FD; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 107 | static int |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 108 | mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg) |
| 109 | { |
| 110 | int rv; |
| 111 | u8 try = 0; |
| 112 | while (try < 100) |
| 113 | { |
| 114 | rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq, |
| 115 | SESSION_MQ_CTRL_EVT_RING, |
| 116 | SVM_Q_NOWAIT, msg); |
| 117 | if (!rv) |
| 118 | return 0; |
| 119 | try++; |
Florin Coras | e2ea193 | 2018-12-17 23:08:14 -0800 | [diff] [blame] | 120 | usleep (1); |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 121 | } |
Florin Coras | dc2e251 | 2018-12-03 17:47:26 -0800 | [diff] [blame] | 122 | clib_warning ("failed to alloc msg"); |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 127 | mq_send_session_accepted_cb (session_t * s) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 128 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 129 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 130 | svm_msg_q_msg_t _msg, *msg = &_msg; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 131 | svm_msg_q_t *app_mq; |
| 132 | fifo_segment_t *eq_seg; |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 133 | session_t *listener; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 134 | session_accepted_msg_t *mp; |
| 135 | session_event_t *evt; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 136 | application_t *app; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 137 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 138 | app = application_get (app_wrk->app_index); |
| 139 | app_mq = app_wrk->event_queue; |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 140 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 141 | return SESSION_E_MQ_MSG_ALLOC; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 142 | |
| 143 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 144 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 145 | evt->event_type = SESSION_CTRL_EVT_ACCEPTED; |
| 146 | mp = (session_accepted_msg_t *) evt->data; |
Florin Coras | 87c6570 | 2019-01-26 14:33:26 -0800 | [diff] [blame] | 147 | clib_memset (mp, 0, sizeof (*mp)); |
Florin Coras | ab2f6db | 2018-08-31 14:31:41 -0700 | [diff] [blame] | 148 | mp->context = app->app_index; |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 149 | mp->server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo); |
| 150 | mp->server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 151 | mp->segment_handle = session_segment_handle (s); |
Nathan Skrzypczak | c00f480 | 2019-12-03 16:25:11 +0100 | [diff] [blame] | 152 | mp->flags = s->flags; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 153 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 154 | eq_seg = session_main_get_evt_q_segment (); |
| 155 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 156 | if (session_has_transport (s)) |
| 157 | { |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 158 | listener = listen_session_get_from_handle (s->listener_handle); |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 159 | mp->listener_handle = app_listen_session_handle (listener); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 160 | if (application_is_proxy (app)) |
| 161 | { |
| 162 | listener = |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 163 | app_worker_first_listener (app_wrk, session_get_fib_proto (s), |
| 164 | session_get_transport_proto (s)); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 165 | if (listener) |
| 166 | mp->listener_handle = listen_session_get_handle (listener); |
| 167 | } |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 168 | mp->vpp_event_queue_address = |
| 169 | fifo_segment_msg_q_offset (eq_seg, s->thread_index); |
| 170 | mp->mq_index = s->thread_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 171 | mp->handle = session_handle (s); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 172 | |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 173 | session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ ); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 177 | ct_connection_t *ct; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 178 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 179 | ct = (ct_connection_t *) session_get_transport (s); |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 180 | listener = listen_session_get_from_handle (s->listener_handle); |
Florin Coras | 87d6633 | 2019-06-11 12:31:31 -0700 | [diff] [blame] | 181 | mp->listener_handle = app_listen_session_handle (listener); |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 182 | mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type); |
| 183 | mp->rmt.port = ct->c_rmt_port; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 184 | mp->handle = session_handle (s); |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 185 | mp->vpp_event_queue_address = |
| 186 | fifo_segment_msg_q_offset (eq_seg, s->thread_index); |
| 187 | mp->mq_index = s->thread_index; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 188 | } |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 189 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 194 | static inline void |
| 195 | mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh, |
| 196 | session_evt_type_t evt_type) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 197 | { |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 198 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 199 | session_disconnected_msg_t *mp; |
| 200 | svm_msg_q_t *app_mq; |
| 201 | session_event_t *evt; |
| 202 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 203 | app_mq = app_wrk->event_queue; |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 204 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 205 | return; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 206 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 207 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 208 | evt->event_type = evt_type; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 209 | mp = (session_disconnected_msg_t *) evt->data; |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 210 | mp->handle = sh; |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 211 | mp->context = app_wrk->api_client_index; |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 212 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 215 | static inline void |
| 216 | mq_notify_close_subscribers (u32 app_index, session_handle_t sh, |
| 217 | svm_fifo_t * f, session_evt_type_t evt_type) |
| 218 | { |
| 219 | app_worker_t *app_wrk; |
| 220 | application_t *app; |
| 221 | int i; |
| 222 | |
| 223 | app = application_get (app_index); |
| 224 | if (!app) |
| 225 | return; |
| 226 | |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 227 | for (i = 0; i < f->shr->n_subscribers; i++) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 228 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 229 | if (!(app_wrk = application_get_worker (app, f->shr->subscribers[i]))) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 230 | continue; |
| 231 | mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 236 | mq_send_session_disconnected_cb (session_t * s) |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 237 | { |
| 238 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
| 239 | session_handle_t sh = session_handle (s); |
| 240 | |
| 241 | mq_send_session_close_evt (app_wrk, session_handle (s), |
| 242 | SESSION_CTRL_EVT_DISCONNECTED); |
| 243 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 244 | if (svm_fifo_n_subscribers (s->rx_fifo)) |
| 245 | mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo, |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 246 | SESSION_CTRL_EVT_DISCONNECTED); |
| 247 | } |
| 248 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 249 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 250 | mq_send_session_reset_cb (session_t * s) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 251 | { |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 252 | app_worker_t *app_wrk = app_worker_get (s->app_wrk_index); |
| 253 | session_handle_t sh = session_handle (s); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 254 | |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 255 | mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET); |
| 256 | |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 257 | if (svm_fifo_n_subscribers (s->rx_fifo)) |
| 258 | mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo, |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 259 | SESSION_CTRL_EVT_RESET); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 262 | int |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 263 | mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context, |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 264 | session_t * s, session_error_t err) |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 265 | { |
| 266 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 267 | session_connected_msg_t *mp; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 268 | svm_msg_q_t *app_mq; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 269 | transport_connection_t *tc; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 270 | fifo_segment_t *eq_seg; |
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 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 296 | eq_seg = session_main_get_evt_q_segment (); |
| 297 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 298 | if (session_has_transport (s)) |
| 299 | { |
| 300 | tc = session_get_transport (s); |
| 301 | if (!tc) |
| 302 | { |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 303 | clib_warning ("failed to retrieve transport!"); |
| 304 | err = SESSION_E_REFUSED; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 305 | goto done; |
| 306 | } |
| 307 | |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 308 | mp->handle = session_handle (s); |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 309 | mp->vpp_event_queue_address = |
| 310 | fifo_segment_msg_q_offset (eq_seg, s->thread_index); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 311 | |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 312 | session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ ); |
Aloys Augustin | cdb7170 | 2019-04-08 17:54:39 +0200 | [diff] [blame] | 313 | |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 314 | mp->server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo); |
| 315 | mp->server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 316 | mp->segment_handle = session_segment_handle (s); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 317 | } |
| 318 | else |
| 319 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 320 | ct_connection_t *cct; |
| 321 | session_t *ss; |
Florin Coras | 9936831 | 2018-08-02 10:45:44 -0700 | [diff] [blame] | 322 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 323 | cct = (ct_connection_t *) session_get_transport (s); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 324 | mp->handle = session_handle (s); |
Florin Coras | 09d18c2 | 2019-04-24 11:10:02 -0700 | [diff] [blame] | 325 | mp->lcl.port = cct->c_lcl_port; |
| 326 | mp->lcl.is_ip4 = cct->c_is_ip4; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 327 | mp->vpp_event_queue_address = |
| 328 | fifo_segment_msg_q_offset (eq_seg, s->thread_index); |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 329 | mp->server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo); |
| 330 | mp->server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 331 | mp->segment_handle = session_segment_handle (s); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 332 | ss = ct_session_get_peer (s); |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 333 | mp->ct_rx_fifo = fifo_segment_fifo_offset (ss->tx_fifo); |
| 334 | mp->ct_tx_fifo = fifo_segment_fifo_offset (ss->rx_fifo); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 335 | mp->ct_segment_handle = session_segment_handle (ss); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | done: |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 339 | mp->retval = err; |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 340 | |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 341 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 52207f1 | 2018-07-12 14:48:06 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 345 | int |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 346 | mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context, |
| 347 | session_handle_t handle, int rv) |
| 348 | { |
| 349 | svm_msg_q_msg_t _msg, *msg = &_msg; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 350 | svm_msg_q_t *app_mq; |
Yu Ping | 0b81915 | 2019-05-07 02:24:30 +0800 | [diff] [blame] | 351 | transport_endpoint_t tep; |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 352 | session_bound_msg_t *mp; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 353 | fifo_segment_t *eq_seg; |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 354 | app_worker_t *app_wrk; |
| 355 | session_event_t *evt; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 356 | app_listener_t *al; |
| 357 | session_t *ls = 0; |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 358 | app_wrk = app_worker_get (app_wrk_index); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 359 | app_mq = app_wrk->event_queue; |
| 360 | if (!app_mq) |
| 361 | { |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 362 | clib_warning ("app %u with api index: %u not attached", |
| 363 | app_wrk->app_index, app_wrk->api_client_index); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 364 | return -1; |
| 365 | } |
| 366 | |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 367 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 368 | return SESSION_E_MQ_MSG_ALLOC; |
Florin Coras | 83ea669 | 2018-10-12 16:55:14 -0700 | [diff] [blame] | 369 | |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 370 | evt = svm_msg_q_msg_data (app_mq, msg); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 371 | clib_memset (evt, 0, sizeof (*evt)); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 372 | evt->event_type = SESSION_CTRL_EVT_BOUND; |
| 373 | mp = (session_bound_msg_t *) evt->data; |
| 374 | mp->context = api_context; |
| 375 | |
| 376 | if (rv) |
| 377 | goto done; |
| 378 | |
| 379 | mp->handle = handle; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 380 | al = app_listener_get_w_handle (handle); |
| 381 | if (al->session_index != SESSION_INVALID_INDEX) |
| 382 | ls = app_listener_get_session (al); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 383 | else |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 384 | ls = app_listener_get_local_session (al); |
Yu Ping | 0b81915 | 2019-05-07 02:24:30 +0800 | [diff] [blame] | 385 | |
| 386 | session_get_endpoint (ls, &tep, 1 /* is_lcl */ ); |
| 387 | mp->lcl_port = tep.port; |
| 388 | mp->lcl_is_ip4 = tep.is_ip4; |
| 389 | clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip)); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 390 | |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 391 | eq_seg = session_main_get_evt_q_segment (); |
| 392 | mp->vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, ls->thread_index); |
Florin Coras | 5f45e01 | 2019-01-23 09:21:30 -0800 | [diff] [blame] | 393 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 394 | if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL) |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 395 | { |
Florin Coras | 14f066e | 2020-12-10 18:52:40 -0800 | [diff] [blame] | 396 | mp->rx_fifo = fifo_segment_fifo_offset (ls->rx_fifo); |
| 397 | mp->tx_fifo = fifo_segment_fifo_offset (ls->tx_fifo); |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 398 | mp->segment_handle = session_segment_handle (ls); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | done: |
| 402 | mp->retval = rv; |
Florin Coras | 537b17e | 2018-09-28 10:35:45 -0700 | [diff] [blame] | 403 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 6011699 | 2018-08-27 09:52:18 -0700 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 407 | void |
| 408 | mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh, |
| 409 | u32 context, int rv) |
| 410 | { |
| 411 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 412 | session_unlisten_reply_msg_t *ump; |
| 413 | svm_msg_q_t *app_mq; |
| 414 | session_event_t *evt; |
| 415 | |
| 416 | app_mq = app_wrk->event_queue; |
| 417 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 418 | return; |
| 419 | |
| 420 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 421 | clib_memset (evt, 0, sizeof (*evt)); |
| 422 | evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY; |
| 423 | ump = (session_unlisten_reply_msg_t *) evt->data; |
| 424 | ump->context = context; |
| 425 | ump->handle = sh; |
| 426 | ump->retval = rv; |
| 427 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 428 | } |
| 429 | |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 430 | static void |
| 431 | mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh) |
| 432 | { |
Florin Coras | 68b7e58 | 2020-01-21 18:33:23 -0800 | [diff] [blame] | 433 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 434 | session_migrated_msg_t *mp; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 435 | fifo_segment_t *eq_seg; |
Florin Coras | 68b7e58 | 2020-01-21 18:33:23 -0800 | [diff] [blame] | 436 | app_worker_t *app_wrk; |
| 437 | session_event_t *evt; |
| 438 | svm_msg_q_t *app_mq; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 439 | u32 thread_index; |
| 440 | |
| 441 | thread_index = session_thread_from_handle (new_sh); |
| 442 | eq_seg = session_main_get_evt_q_segment (); |
Florin Coras | 68b7e58 | 2020-01-21 18:33:23 -0800 | [diff] [blame] | 443 | |
| 444 | app_wrk = app_worker_get (s->app_wrk_index); |
| 445 | app_mq = app_wrk->event_queue; |
| 446 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 447 | return; |
| 448 | |
| 449 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 450 | clib_memset (evt, 0, sizeof (*evt)); |
| 451 | evt->event_type = SESSION_CTRL_EVT_MIGRATED; |
| 452 | mp = (session_migrated_msg_t *) evt->data; |
| 453 | mp->handle = session_handle (s); |
| 454 | mp->new_handle = new_sh; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 455 | mp->vpp_thread_index = thread_index; |
| 456 | mp->vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, thread_index); |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 457 | mp->segment_handle = session_segment_handle (s); |
Florin Coras | 68b7e58 | 2020-01-21 18:33:23 -0800 | [diff] [blame] | 458 | svm_msg_q_add_and_unlock (app_mq, msg); |
Florin Coras | 49568af | 2019-07-31 16:46:24 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 461 | static int |
| 462 | mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle) |
| 463 | { |
| 464 | int fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 465 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 466 | session_app_add_segment_msg_t *mp; |
| 467 | vl_api_registration_t *reg; |
| 468 | app_worker_t *app_wrk; |
| 469 | session_event_t *evt; |
| 470 | svm_msg_q_t *app_mq; |
| 471 | fifo_segment_t *fs; |
| 472 | ssvm_private_t *sp; |
| 473 | u8 fd_flags = 0; |
| 474 | |
| 475 | app_wrk = app_worker_get (app_wrk_index); |
| 476 | |
| 477 | reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index); |
| 478 | if (!reg) |
| 479 | { |
| 480 | clib_warning ("no api registration for client: %u", |
| 481 | app_wrk->api_client_index); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | fs = segment_manager_get_segment_w_handle (segment_handle); |
| 486 | sp = &fs->ssvm; |
| 487 | if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD) |
| 488 | { |
| 489 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
| 490 | { |
| 491 | clib_warning ("can't send memfd fd"); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 496 | fds[n_fds] = sp->fd; |
| 497 | n_fds += 1; |
| 498 | } |
| 499 | |
| 500 | app_mq = app_wrk->event_queue; |
| 501 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 502 | return -1; |
| 503 | |
| 504 | if (n_fds) |
| 505 | session_send_fds (reg, fds, n_fds); |
| 506 | |
| 507 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 508 | clib_memset (evt, 0, sizeof (*evt)); |
| 509 | evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT; |
| 510 | mp = (session_app_add_segment_msg_t *) evt->data; |
| 511 | clib_memset (mp, 0, sizeof (*mp)); |
| 512 | mp->segment_size = sp->ssvm_size; |
| 513 | mp->fd_flags = fd_flags; |
| 514 | mp->segment_handle = segment_handle; |
| 515 | strncpy ((char *) mp->segment_name, (char *) sp->name, |
| 516 | sizeof (mp->segment_name) - 1); |
| 517 | |
| 518 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int |
| 524 | mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle) |
| 525 | { |
| 526 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 527 | session_app_del_segment_msg_t *mp; |
| 528 | vl_api_registration_t *reg; |
| 529 | app_worker_t *app_wrk; |
| 530 | session_event_t *evt; |
| 531 | svm_msg_q_t *app_mq; |
| 532 | |
| 533 | app_wrk = app_worker_get (app_wrk_index); |
| 534 | reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index); |
| 535 | if (!reg) |
| 536 | { |
| 537 | clib_warning ("no registration: %u", app_wrk->api_client_index); |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | app_mq = app_wrk->event_queue; |
| 542 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 543 | return -1; |
| 544 | |
| 545 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 546 | clib_memset (evt, 0, sizeof (*evt)); |
| 547 | evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT; |
| 548 | mp = (session_app_del_segment_msg_t *) evt->data; |
| 549 | clib_memset (mp, 0, sizeof (*mp)); |
| 550 | mp->segment_handle = segment_handle; |
| 551 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
Florin Coras | 9ace36d | 2019-10-28 13:14:17 -0700 | [diff] [blame] | 556 | static void |
| 557 | mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf) |
| 558 | { |
| 559 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 560 | session_cleanup_msg_t *mp; |
| 561 | svm_msg_q_t *app_mq; |
| 562 | session_event_t *evt; |
| 563 | app_worker_t *app_wrk; |
| 564 | |
Florin Coras | 36d4939 | 2020-04-24 23:00:11 +0000 | [diff] [blame] | 565 | /* Propagate transport cleanup notifications only if app didn't close */ |
| 566 | if (ntf == SESSION_CLEANUP_TRANSPORT |
| 567 | && s->session_state != SESSION_STATE_TRANSPORT_DELETED) |
Florin Coras | 9ace36d | 2019-10-28 13:14:17 -0700 | [diff] [blame] | 568 | return; |
| 569 | |
| 570 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 571 | if (!app_wrk) |
| 572 | return; |
| 573 | |
| 574 | app_mq = app_wrk->event_queue; |
| 575 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 576 | return; |
| 577 | |
| 578 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 579 | clib_memset (evt, 0, sizeof (*evt)); |
| 580 | evt->event_type = SESSION_CTRL_EVT_CLEANUP; |
| 581 | mp = (session_cleanup_msg_t *) evt->data; |
| 582 | mp->handle = session_handle (s); |
Florin Coras | 36d4939 | 2020-04-24 23:00:11 +0000 | [diff] [blame] | 583 | mp->type = ntf; |
Florin Coras | 9ace36d | 2019-10-28 13:14:17 -0700 | [diff] [blame] | 584 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 585 | } |
| 586 | |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 587 | static session_cb_vft_t session_mq_cb_vft = { |
| 588 | .session_accept_callback = mq_send_session_accepted_cb, |
| 589 | .session_disconnect_callback = mq_send_session_disconnected_cb, |
| 590 | .session_connected_callback = mq_send_session_connected_cb, |
| 591 | .session_reset_callback = mq_send_session_reset_cb, |
| 592 | .session_migrate_callback = mq_send_session_migrate_cb, |
Florin Coras | 9ace36d | 2019-10-28 13:14:17 -0700 | [diff] [blame] | 593 | .session_cleanup_callback = mq_send_session_cleanup_cb, |
Florin Coras | c4c4cf5 | 2019-08-24 18:17:34 -0700 | [diff] [blame] | 594 | .add_segment_callback = mq_send_add_segment_cb, |
| 595 | .del_segment_callback = mq_send_del_segment_cb, |
| 596 | }; |
| 597 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 598 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 599 | vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) |
| 600 | { |
| 601 | vl_api_session_enable_disable_reply_t *rmp; |
| 602 | vlib_main_t *vm = vlib_get_main (); |
| 603 | int rv = 0; |
| 604 | |
| 605 | vnet_session_enable_disable (vm, mp->is_enable); |
| 606 | REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY); |
| 607 | } |
| 608 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 609 | static void |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 610 | vl_api_app_attach_t_handler (vl_api_app_attach_t * mp) |
| 611 | { |
| 612 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 613 | vl_api_app_attach_reply_t *rmp; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 614 | fifo_segment_t *segp, *evt_q_segment = 0; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 615 | vnet_app_attach_args_t _a, *a = &_a; |
| 616 | u8 fd_flags = 0, ctrl_thread; |
| 617 | vl_api_registration_t *reg; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 618 | |
| 619 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 620 | if (!reg) |
| 621 | return; |
| 622 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 623 | if (!session_main_is_enabled () || appns_sapi_enabled ()) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 624 | { |
| 625 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 626 | goto done; |
| 627 | } |
Florin Coras | 6c10ab2 | 2020-11-08 16:50:39 -0800 | [diff] [blame] | 628 | /* Only support binary api with socket transport */ |
| 629 | if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI) |
| 630 | { |
| 631 | rv = VNET_API_ERROR_APP_UNSUPPORTED_CFG; |
| 632 | goto done; |
| 633 | } |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 634 | |
| 635 | STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <= |
| 636 | sizeof (mp->options), |
| 637 | "Out of options, fix api message definition"); |
| 638 | |
| 639 | clib_memset (a, 0, sizeof (*a)); |
| 640 | a->api_client_index = mp->client_index; |
| 641 | a->options = mp->options; |
| 642 | a->session_cb_vft = &session_mq_cb_vft; |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 643 | a->namespace_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 644 | |
| 645 | if ((rv = vnet_application_attach (a))) |
| 646 | { |
| 647 | clib_warning ("attach returned: %d", rv); |
| 648 | vec_free (a->namespace_id); |
| 649 | goto done; |
| 650 | } |
| 651 | vec_free (a->namespace_id); |
| 652 | |
| 653 | /* Send event queues segment */ |
| 654 | if ((evt_q_segment = session_main_get_evt_q_segment ())) |
| 655 | { |
| 656 | fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT; |
Florin Coras | 04943b4 | 2020-12-25 11:45:40 -0800 | [diff] [blame] | 657 | fds[n_fds] = evt_q_segment->ssvm.fd; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 658 | n_fds += 1; |
| 659 | } |
| 660 | /* Send fifo segment fd if needed */ |
| 661 | if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) |
| 662 | { |
| 663 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 664 | fds[n_fds] = a->segment->fd; |
| 665 | n_fds += 1; |
| 666 | } |
| 667 | if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 668 | { |
| 669 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 670 | fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 671 | n_fds += 1; |
| 672 | } |
| 673 | |
| 674 | done: |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 675 | /* *INDENT-OFF* */ |
| 676 | REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({ |
| 677 | if (!rv) |
| 678 | { |
Vratko Polak | 4f59985 | 2019-11-08 10:32:39 +0100 | [diff] [blame] | 679 | ctrl_thread = vlib_num_workers () ? 1 : 0; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 680 | segp = (fifo_segment_t *) a->segment; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 681 | rmp->app_index = clib_host_to_net_u32 (a->app_index); |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 682 | rmp->app_mq = fifo_segment_msg_q_offset (segp, 0); |
| 683 | rmp->vpp_ctrl_mq = |
| 684 | fifo_segment_msg_q_offset (evt_q_segment, ctrl_thread); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 685 | rmp->vpp_ctrl_mq_thread = ctrl_thread; |
| 686 | rmp->n_fds = n_fds; |
| 687 | rmp->fd_flags = fd_flags; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 688 | if (vec_len (segp->ssvm.name)) |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 689 | { |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 690 | vl_api_vec_to_api_string (segp->ssvm.name, &rmp->segment_name); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 691 | } |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 692 | rmp->segment_size = segp->ssvm.ssvm_size; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 693 | rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle); |
| 694 | } |
| 695 | })); |
| 696 | /* *INDENT-ON* */ |
| 697 | |
| 698 | if (n_fds) |
| 699 | session_send_fds (reg, fds, n_fds); |
| 700 | } |
| 701 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 702 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 703 | vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp) |
| 704 | { |
| 705 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 706 | vl_api_app_worker_add_del_reply_t *rmp; |
| 707 | vl_api_registration_t *reg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 708 | application_t *app; |
| 709 | u8 fd_flags = 0; |
| 710 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 711 | if (!session_main_is_enabled () || appns_sapi_enabled ()) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 712 | { |
| 713 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 714 | goto done; |
| 715 | } |
| 716 | |
| 717 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 718 | if (!reg) |
| 719 | return; |
| 720 | |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 721 | 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] | 722 | if (!app) |
| 723 | { |
| 724 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 725 | goto done; |
| 726 | } |
| 727 | |
| 728 | vnet_app_worker_add_del_args_t args = { |
| 729 | .app_index = app->app_index, |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 730 | .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index), |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 731 | .api_client_index = mp->client_index, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 732 | .is_add = mp->is_add |
| 733 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 734 | rv = vnet_app_worker_add_del (&args); |
| 735 | if (rv) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 736 | { |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 737 | clib_warning ("app worker add/del returned: %d", rv); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 738 | goto done; |
| 739 | } |
| 740 | |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 741 | if (!mp->is_add) |
| 742 | goto done; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 743 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 744 | /* Send fifo segment fd if needed */ |
| 745 | if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) |
| 746 | { |
| 747 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 748 | fds[n_fds] = args.segment->fd; |
| 749 | n_fds += 1; |
| 750 | } |
| 751 | if (application_segment_manager_properties (app)->use_mq_eventfd) |
| 752 | { |
| 753 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 754 | fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 755 | n_fds += 1; |
| 756 | } |
| 757 | |
| 758 | /* *INDENT-OFF* */ |
| 759 | done: |
| 760 | REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({ |
| 761 | rmp->is_add = mp->is_add; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 762 | rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 763 | rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle); |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 764 | if (!rv && mp->is_add) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 765 | { |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 766 | rmp->app_event_queue_address = |
| 767 | fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0); |
| 768 | rmp->n_fds = n_fds; |
| 769 | rmp->fd_flags = fd_flags; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 770 | if (vec_len (args.segment->name)) |
| 771 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 772 | vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 773 | } |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 774 | } |
| 775 | })); |
| 776 | /* *INDENT-ON* */ |
| 777 | |
| 778 | if (n_fds) |
| 779 | session_send_fds (reg, fds, n_fds); |
| 780 | } |
| 781 | |
| 782 | static void |
Florin Coras | 888d9f0 | 2020-04-02 23:00:13 +0000 | [diff] [blame] | 783 | vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) |
| 784 | { |
| 785 | vl_api_application_detach_reply_t *rmp; |
| 786 | int rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 787 | vnet_app_detach_args_t _a, *a = &_a; |
| 788 | application_t *app; |
| 789 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 790 | if (!session_main_is_enabled () || appns_sapi_enabled ()) |
Florin Coras | 888d9f0 | 2020-04-02 23:00:13 +0000 | [diff] [blame] | 791 | { |
| 792 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 793 | goto done; |
| 794 | } |
| 795 | |
| 796 | app = application_lookup (mp->client_index); |
| 797 | if (app) |
| 798 | { |
| 799 | a->app_index = app->app_index; |
| 800 | a->api_client_index = mp->client_index; |
| 801 | rv = vnet_application_detach (a); |
| 802 | } |
| 803 | |
| 804 | done: |
| 805 | REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); |
| 806 | } |
| 807 | |
| 808 | static void |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 809 | vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) |
| 810 | { |
| 811 | vl_api_app_namespace_add_del_reply_t *rmp; |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 812 | u32 appns_index = 0; |
| 813 | u8 *ns_id = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 814 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 815 | if (session_main_is_enabled () == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 816 | { |
| 817 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 818 | goto done; |
| 819 | } |
| 820 | |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 821 | ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 822 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 823 | vnet_app_namespace_add_del_args_t args = { |
| 824 | .ns_id = ns_id, |
Florin Coras | 9a9adb2 | 2017-10-26 08:16:59 -0700 | [diff] [blame] | 825 | .secret = clib_net_to_host_u64 (mp->secret), |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 826 | .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index), |
| 827 | .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id), |
| 828 | .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id), |
| 829 | .is_add = 1 |
| 830 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 831 | rv = vnet_app_namespace_add_del (&args); |
| 832 | if (!rv) |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 833 | { |
| 834 | appns_index = app_namespace_index_from_id (ns_id); |
| 835 | if (appns_index == APP_NAMESPACE_INVALID_INDEX) |
| 836 | { |
| 837 | clib_warning ("app ns lookup failed"); |
| 838 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 839 | } |
| 840 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 841 | vec_free (ns_id); |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 842 | |
| 843 | /* *INDENT-OFF* */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 844 | done: |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 845 | REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({ |
| 846 | if (!rv) |
| 847 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
| 848 | })); |
| 849 | /* *INDENT-ON* */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 852 | static void |
| 853 | vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp) |
| 854 | { |
| 855 | vl_api_session_rule_add_del_reply_t *rmp; |
| 856 | session_rule_add_del_args_t args; |
| 857 | session_rule_table_add_del_args_t *table_args = &args.table_args; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 858 | int rv = 0; |
| 859 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 860 | clib_memset (&args, 0, sizeof (args)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 861 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 862 | ip_prefix_decode (&mp->lcl, &table_args->lcl); |
| 863 | ip_prefix_decode (&mp->rmt, &table_args->rmt); |
| 864 | |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 865 | table_args->lcl_port = mp->lcl_port; |
| 866 | table_args->rmt_port = mp->rmt_port; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 867 | table_args->action_index = clib_net_to_host_u32 (mp->action_index); |
| 868 | table_args->is_add = mp->is_add; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 869 | mp->tag[sizeof (mp->tag) - 1] = 0; |
| 870 | table_args->tag = format (0, "%s", mp->tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 871 | args.appns_index = clib_net_to_host_u32 (mp->appns_index); |
| 872 | args.scope = mp->scope; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 873 | args.transport_proto = |
| 874 | api_session_transport_proto_decode (&mp->transport_proto) == |
| 875 | TRANSPORT_PROTO_UDP ? 1 : 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 876 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 877 | rv = vnet_session_rule_add_del (&args); |
| 878 | if (rv) |
| 879 | clib_warning ("rule add del returned: %d", rv); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 880 | vec_free (table_args->tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 881 | REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY); |
| 882 | } |
| 883 | |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 884 | static void |
| 885 | send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 886 | u8 transport_proto, u32 appns_index, u8 * tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 887 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 888 | { |
| 889 | vl_api_session_rules_details_t *rmp = 0; |
| 890 | session_mask_or_match_4_t *match = |
| 891 | (session_mask_or_match_4_t *) & rule->match; |
| 892 | session_mask_or_match_4_t *mask = |
| 893 | (session_mask_or_match_4_t *) & rule->mask; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 894 | fib_prefix_t lcl, rmt; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 895 | |
| 896 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 897 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 898 | rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); |
| 899 | rmp->context = context; |
| 900 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 901 | clib_memset (&lcl, 0, sizeof (lcl)); |
| 902 | clib_memset (&rmt, 0, sizeof (rmt)); |
| 903 | ip_set (&lcl.fp_addr, &match->lcl_ip, 1); |
| 904 | ip_set (&rmt.fp_addr, &match->rmt_ip, 1); |
| 905 | lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip); |
| 906 | rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip); |
| 907 | |
| 908 | ip_prefix_encode (&lcl, &rmp->lcl); |
| 909 | ip_prefix_encode (&rmt, &rmp->rmt); |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 910 | rmp->lcl_port = match->lcl_port; |
| 911 | rmp->rmt_port = match->rmt_port; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 912 | rmp->action_index = clib_host_to_net_u32 (rule->action_index); |
| 913 | rmp->scope = |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 914 | is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL; |
| 915 | rmp->transport_proto = api_session_transport_proto_encode (transport_proto); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 916 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 917 | if (tag) |
| 918 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 919 | clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 920 | rmp->tag[vec_len (tag)] = 0; |
| 921 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 922 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 923 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static void |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 927 | send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local, |
| 928 | u8 transport_proto, u32 appns_index, u8 * tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 929 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 930 | { |
| 931 | vl_api_session_rules_details_t *rmp = 0; |
| 932 | session_mask_or_match_6_t *match = |
| 933 | (session_mask_or_match_6_t *) & rule->match; |
| 934 | session_mask_or_match_6_t *mask = |
| 935 | (session_mask_or_match_6_t *) & rule->mask; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 936 | fib_prefix_t lcl, rmt; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 937 | |
| 938 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 939 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 940 | rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); |
| 941 | rmp->context = context; |
| 942 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 943 | clib_memset (&lcl, 0, sizeof (lcl)); |
| 944 | clib_memset (&rmt, 0, sizeof (rmt)); |
| 945 | ip_set (&lcl.fp_addr, &match->lcl_ip, 0); |
| 946 | ip_set (&rmt.fp_addr, &match->rmt_ip, 0); |
| 947 | lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip); |
| 948 | rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip); |
| 949 | |
| 950 | ip_prefix_encode (&lcl, &rmp->lcl); |
| 951 | ip_prefix_encode (&rmt, &rmp->rmt); |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 952 | rmp->lcl_port = match->lcl_port; |
| 953 | rmp->rmt_port = match->rmt_port; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 954 | rmp->action_index = clib_host_to_net_u32 (rule->action_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 955 | rmp->scope = |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 956 | is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL; |
| 957 | rmp->transport_proto = api_session_transport_proto_encode (transport_proto); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 958 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 959 | if (tag) |
| 960 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 961 | clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 962 | rmp->tag[vec_len (tag)] = 0; |
| 963 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 964 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 965 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static void |
| 969 | 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] | 970 | u8 tp, u8 is_local, u32 appns_index, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 971 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 972 | { |
| 973 | mma_rule_16_t *rule16; |
| 974 | mma_rule_40_t *rule40; |
| 975 | mma_rules_table_16_t *srt16; |
| 976 | mma_rules_table_40_t *srt40; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 977 | u32 ri; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 978 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 979 | if (is_local || fib_proto == FIB_PROTOCOL_IP4) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 980 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 981 | u8 *tag = 0; |
| 982 | /* *INDENT-OFF* */ |
| 983 | srt16 = &srt->session_rules_tables_16; |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 984 | pool_foreach (rule16, srt16->rules) { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 985 | ri = mma_rules_table_rule_index_16 (srt16, rule16); |
| 986 | tag = session_rules_table_rule_tag (srt, ri, 1); |
| 987 | send_session_rule_details4 (rule16, is_local, tp, appns_index, tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 988 | reg, context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 989 | } |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 990 | /* *INDENT-ON* */ |
| 991 | } |
| 992 | if (is_local || fib_proto == FIB_PROTOCOL_IP6) |
| 993 | { |
| 994 | u8 *tag = 0; |
| 995 | /* *INDENT-OFF* */ |
| 996 | srt40 = &srt->session_rules_tables_40; |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 997 | pool_foreach (rule40, srt40->rules) { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 998 | ri = mma_rules_table_rule_index_40 (srt40, rule40); |
| 999 | tag = session_rules_table_rule_tag (srt, ri, 1); |
| 1000 | send_session_rule_details6 (rule40, is_local, tp, appns_index, tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1001 | reg, context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1002 | } |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1003 | /* *INDENT-ON* */ |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | static void |
Neale Ranns | 2b202bc | 2020-09-21 08:17:51 +0000 | [diff] [blame] | 1008 | vl_api_session_rules_dump_t_handler (vl_api_session_rules_dump_t * mp) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1009 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1010 | vl_api_registration_t *reg; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1011 | session_table_t *st; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1012 | u8 tp; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1013 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1014 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 1015 | if (!reg) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1016 | return; |
| 1017 | |
| 1018 | /* *INDENT-OFF* */ |
| 1019 | session_table_foreach (st, ({ |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 1020 | for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1021 | { |
| 1022 | send_session_rules_table_details (&st->session_rules[tp], |
| 1023 | st->active_fib_proto, tp, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1024 | st->is_local, st->appns_index, reg, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1025 | mp->context); |
| 1026 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1027 | })); |
| 1028 | /* *INDENT-ON* */ |
| 1029 | } |
| 1030 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1031 | static void |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1032 | 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] | 1033 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1034 | vl_api_app_add_cert_key_pair_reply_t *rmp; |
| 1035 | vnet_app_add_cert_key_pair_args_t _a, *a = &_a; |
| 1036 | u32 certkey_len, key_len, cert_len; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1037 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1038 | if (session_main_is_enabled () == 0) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1039 | { |
| 1040 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1041 | goto done; |
| 1042 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1043 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1044 | cert_len = clib_net_to_host_u16 (mp->cert_len); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 1045 | if (cert_len > 10000) |
| 1046 | { |
| 1047 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1048 | goto done; |
| 1049 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1050 | |
| 1051 | certkey_len = clib_net_to_host_u16 (mp->certkey_len); |
| 1052 | if (certkey_len < cert_len) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1053 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1054 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1055 | goto done; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1056 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1057 | |
| 1058 | key_len = certkey_len - cert_len; |
| 1059 | if (key_len > 10000) |
| 1060 | { |
| 1061 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
| 1065 | clib_memset (a, 0, sizeof (*a)); |
Florin Coras | a5a9efd | 2021-01-05 17:03:29 -0800 | [diff] [blame] | 1066 | a->cert = mp->certkey; |
| 1067 | a->key = mp->certkey + cert_len; |
| 1068 | a->cert_len = cert_len; |
| 1069 | a->key_len = key_len; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1070 | rv = vnet_app_add_cert_key_pair (a); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1071 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1072 | done: |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1073 | /* *INDENT-OFF* */ |
| 1074 | REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({ |
| 1075 | if (!rv) |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1076 | rmp->index = clib_host_to_net_u32 (a->index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1077 | })); |
| 1078 | /* *INDENT-ON* */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static void |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1082 | 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] | 1083 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1084 | vl_api_app_del_cert_key_pair_reply_t *rmp; |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1085 | u32 ckpair_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1086 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1087 | if (session_main_is_enabled () == 0) |
| 1088 | { |
| 1089 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1090 | goto done; |
| 1091 | } |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1092 | ckpair_index = clib_net_to_host_u32 (mp->index); |
| 1093 | rv = vnet_app_del_cert_key_pair (ckpair_index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1094 | |
| 1095 | done: |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1096 | REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1099 | static clib_error_t * |
| 1100 | application_reaper_cb (u32 client_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1101 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1102 | application_t *app = application_lookup (client_index); |
| 1103 | vnet_app_detach_args_t _a, *a = &_a; |
| 1104 | if (app) |
| 1105 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1106 | a->app_index = app->app_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1107 | a->api_client_index = client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1108 | vnet_application_detach (a); |
| 1109 | } |
| 1110 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1111 | } |
| 1112 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1113 | VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1114 | |
| 1115 | #define vl_msg_name_crc_list |
| 1116 | #include <vnet/vnet_all_api_h.h> |
| 1117 | #undef vl_msg_name_crc_list |
| 1118 | |
| 1119 | static void |
| 1120 | setup_message_id_table (api_main_t * am) |
| 1121 | { |
| 1122 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1123 | foreach_vl_msg_name_crc_session; |
| 1124 | #undef _ |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * session_api_hookup |
| 1129 | * Add uri's API message handlers to the table. |
Jim Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 1130 | * vlib has already mapped shared memory and |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1131 | * added the client registration handlers. |
| 1132 | * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() |
| 1133 | */ |
| 1134 | static clib_error_t * |
| 1135 | session_api_hookup (vlib_main_t * vm) |
| 1136 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1137 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1138 | |
| 1139 | #define _(N,n) \ |
| 1140 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1141 | vl_api_##n##_t_handler, \ |
| 1142 | vl_noop_handler, \ |
| 1143 | vl_api_##n##_t_endian, \ |
| 1144 | vl_api_##n##_t_print, \ |
| 1145 | sizeof(vl_api_##n##_t), 1); |
| 1146 | foreach_session_api_msg; |
| 1147 | #undef _ |
| 1148 | |
| 1149 | /* |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1150 | * Set up the (msg_name, crc, message-id) table |
| 1151 | */ |
| 1152 | setup_message_id_table (am); |
| 1153 | |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | VLIB_API_INIT_FUNCTION (session_api_hookup); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1158 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1159 | /* |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1160 | * Socket api functions |
| 1161 | */ |
| 1162 | |
| 1163 | static void |
| 1164 | sapi_send_fds (app_worker_t * app_wrk, int *fds, int n_fds) |
| 1165 | { |
| 1166 | app_sapi_msg_t smsg = { 0 }; |
| 1167 | app_namespace_t *app_ns; |
| 1168 | application_t *app; |
| 1169 | clib_socket_t *cs; |
| 1170 | u32 cs_index; |
| 1171 | |
| 1172 | app = application_get (app_wrk->app_index); |
| 1173 | app_ns = app_namespace_get (app->ns_index); |
| 1174 | cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index); |
| 1175 | cs = appns_sapi_get_socket (app_ns, cs_index); |
Florin Coras | 1c0573d | 2020-09-23 10:31:27 -0700 | [diff] [blame] | 1176 | if (PREDICT_FALSE (!cs)) |
| 1177 | return; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1178 | |
| 1179 | /* There's no payload for the message only the type */ |
| 1180 | smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS; |
| 1181 | clib_socket_sendmsg (cs, &smsg, sizeof (smsg), fds, n_fds); |
| 1182 | } |
| 1183 | |
| 1184 | static int |
| 1185 | mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle) |
| 1186 | { |
| 1187 | int fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 1188 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 1189 | session_app_add_segment_msg_t *mp; |
| 1190 | app_worker_t *app_wrk; |
| 1191 | session_event_t *evt; |
| 1192 | svm_msg_q_t *app_mq; |
| 1193 | fifo_segment_t *fs; |
| 1194 | ssvm_private_t *sp; |
| 1195 | u8 fd_flags = 0; |
| 1196 | |
| 1197 | app_wrk = app_worker_get (app_wrk_index); |
| 1198 | |
| 1199 | fs = segment_manager_get_segment_w_handle (segment_handle); |
| 1200 | sp = &fs->ssvm; |
| 1201 | ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD); |
| 1202 | |
| 1203 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 1204 | fds[n_fds] = sp->fd; |
| 1205 | n_fds += 1; |
| 1206 | |
| 1207 | app_mq = app_wrk->event_queue; |
| 1208 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 1209 | return -1; |
| 1210 | |
| 1211 | /* |
| 1212 | * Send the fd over api socket |
| 1213 | */ |
| 1214 | sapi_send_fds (app_wrk, fds, n_fds); |
| 1215 | |
| 1216 | /* |
| 1217 | * Send the actual message over mq |
| 1218 | */ |
| 1219 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 1220 | clib_memset (evt, 0, sizeof (*evt)); |
| 1221 | evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT; |
| 1222 | mp = (session_app_add_segment_msg_t *) evt->data; |
| 1223 | clib_memset (mp, 0, sizeof (*mp)); |
| 1224 | mp->segment_size = sp->ssvm_size; |
| 1225 | mp->fd_flags = fd_flags; |
| 1226 | mp->segment_handle = segment_handle; |
| 1227 | strncpy ((char *) mp->segment_name, (char *) sp->name, |
| 1228 | sizeof (mp->segment_name) - 1); |
| 1229 | |
| 1230 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | static int |
| 1236 | mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle) |
| 1237 | { |
| 1238 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 1239 | session_app_del_segment_msg_t *mp; |
| 1240 | app_worker_t *app_wrk; |
| 1241 | session_event_t *evt; |
| 1242 | svm_msg_q_t *app_mq; |
| 1243 | |
| 1244 | app_wrk = app_worker_get (app_wrk_index); |
| 1245 | |
| 1246 | app_mq = app_wrk->event_queue; |
| 1247 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 1248 | return -1; |
| 1249 | |
| 1250 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 1251 | clib_memset (evt, 0, sizeof (*evt)); |
| 1252 | evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT; |
| 1253 | mp = (session_app_del_segment_msg_t *) evt->data; |
| 1254 | clib_memset (mp, 0, sizeof (*mp)); |
| 1255 | mp->segment_handle = segment_handle; |
| 1256 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static session_cb_vft_t session_mq_sapi_cb_vft = { |
| 1262 | .session_accept_callback = mq_send_session_accepted_cb, |
| 1263 | .session_disconnect_callback = mq_send_session_disconnected_cb, |
| 1264 | .session_connected_callback = mq_send_session_connected_cb, |
| 1265 | .session_reset_callback = mq_send_session_reset_cb, |
| 1266 | .session_migrate_callback = mq_send_session_migrate_cb, |
| 1267 | .session_cleanup_callback = mq_send_session_cleanup_cb, |
| 1268 | .add_segment_callback = mq_send_add_segment_sapi_cb, |
| 1269 | .del_segment_callback = mq_send_del_segment_sapi_cb, |
| 1270 | }; |
| 1271 | |
| 1272 | static void |
| 1273 | session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs, |
| 1274 | app_sapi_attach_msg_t * mp) |
| 1275 | { |
| 1276 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 1277 | vnet_app_attach_args_t _a, *a = &_a; |
| 1278 | app_sapi_attach_reply_msg_t *rmp; |
Florin Coras | 04943b4 | 2020-12-25 11:45:40 -0800 | [diff] [blame] | 1279 | fifo_segment_t *evt_q_segment; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1280 | u8 fd_flags = 0, ctrl_thread; |
| 1281 | app_ns_api_handle_t *handle; |
| 1282 | app_sapi_msg_t msg = { 0 }; |
| 1283 | app_worker_t *app_wrk; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1284 | application_t *app; |
| 1285 | |
| 1286 | /* Make sure name is null terminated */ |
| 1287 | mp->name[63] = 0; |
| 1288 | |
| 1289 | clib_memset (a, 0, sizeof (*a)); |
| 1290 | a->api_client_index = appns_sapi_socket_handle (app_ns, cs); |
| 1291 | a->name = format (0, "%s", (char *) mp->name); |
| 1292 | a->options = mp->options; |
| 1293 | a->session_cb_vft = &session_mq_sapi_cb_vft; |
| 1294 | a->use_sock_api = 1; |
| 1295 | a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns); |
| 1296 | |
| 1297 | if ((rv = vnet_application_attach (a))) |
| 1298 | { |
| 1299 | clib_warning ("attach returned: %d", rv); |
| 1300 | goto done; |
| 1301 | } |
| 1302 | |
| 1303 | /* Send event queues segment */ |
| 1304 | if ((evt_q_segment = session_main_get_evt_q_segment ())) |
| 1305 | { |
| 1306 | fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT; |
Florin Coras | 04943b4 | 2020-12-25 11:45:40 -0800 | [diff] [blame] | 1307 | fds[n_fds] = evt_q_segment->ssvm.fd; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1308 | n_fds += 1; |
| 1309 | } |
| 1310 | /* Send fifo segment fd if needed */ |
| 1311 | if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) |
| 1312 | { |
| 1313 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 1314 | fds[n_fds] = a->segment->fd; |
| 1315 | n_fds += 1; |
| 1316 | } |
| 1317 | if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 1318 | { |
| 1319 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 1320 | fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1321 | n_fds += 1; |
| 1322 | } |
| 1323 | |
| 1324 | done: |
| 1325 | |
| 1326 | msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY; |
| 1327 | rmp = &msg.attach_reply; |
| 1328 | rmp->retval = rv; |
| 1329 | if (!rv) |
| 1330 | { |
| 1331 | ctrl_thread = vlib_num_workers ()? 1 : 0; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1332 | rmp->app_index = a->app_index; |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 1333 | rmp->app_mq = |
| 1334 | fifo_segment_msg_q_offset ((fifo_segment_t *) a->segment, 0); |
| 1335 | rmp->vpp_ctrl_mq = |
| 1336 | fifo_segment_msg_q_offset (evt_q_segment, ctrl_thread); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1337 | rmp->vpp_ctrl_mq_thread = ctrl_thread; |
| 1338 | rmp->n_fds = n_fds; |
| 1339 | rmp->fd_flags = fd_flags; |
| 1340 | /* No segment name and size since we only support memfds |
| 1341 | * in this configuration */ |
| 1342 | rmp->segment_handle = a->segment_handle; |
| 1343 | rmp->api_client_handle = a->api_client_index; |
| 1344 | |
| 1345 | /* Update app index for socket */ |
| 1346 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1347 | app = application_get (a->app_index); |
| 1348 | app_wrk = application_get_worker (app, 0); |
| 1349 | handle->aah_app_wrk_index = app_wrk->wrk_index; |
| 1350 | } |
| 1351 | |
| 1352 | clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds); |
| 1353 | vec_free (a->name); |
| 1354 | } |
| 1355 | |
| 1356 | static void |
| 1357 | sapi_socket_close_w_handle (u32 api_handle) |
| 1358 | { |
| 1359 | app_namespace_t *app_ns = app_namespace_get (api_handle >> 16); |
| 1360 | u16 sock_index = api_handle & 0xffff; |
| 1361 | app_ns_api_handle_t *handle; |
| 1362 | clib_socket_t *cs; |
| 1363 | clib_file_t *cf; |
| 1364 | |
| 1365 | cs = appns_sapi_get_socket (app_ns, sock_index); |
| 1366 | if (!cs) |
| 1367 | return; |
| 1368 | |
| 1369 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1370 | cf = clib_file_get (&file_main, handle->aah_file_index); |
| 1371 | clib_file_del (&file_main, cf); |
| 1372 | |
| 1373 | clib_socket_close (cs); |
| 1374 | appns_sapi_free_socket (app_ns, cs); |
| 1375 | } |
| 1376 | |
| 1377 | static void |
| 1378 | sapi_add_del_worker_handler (app_namespace_t * app_ns, |
| 1379 | clib_socket_t * cs, |
| 1380 | app_sapi_worker_add_del_msg_t * mp) |
| 1381 | { |
| 1382 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 1383 | app_sapi_worker_add_del_reply_msg_t *rmp; |
| 1384 | app_ns_api_handle_t *handle; |
| 1385 | app_sapi_msg_t msg = { 0 }; |
| 1386 | app_worker_t *app_wrk; |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 1387 | u32 sapi_handle = -1; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1388 | application_t *app; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1389 | u8 fd_flags = 0; |
| 1390 | |
| 1391 | app = application_get_if_valid (mp->app_index); |
| 1392 | if (!app) |
| 1393 | { |
| 1394 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1395 | goto done; |
| 1396 | } |
| 1397 | |
| 1398 | sapi_handle = appns_sapi_socket_handle (app_ns, cs); |
| 1399 | |
| 1400 | vnet_app_worker_add_del_args_t args = { |
| 1401 | .app_index = app->app_index, |
| 1402 | .wrk_map_index = mp->wrk_index, |
| 1403 | .api_client_index = sapi_handle, |
| 1404 | .is_add = mp->is_add |
| 1405 | }; |
| 1406 | rv = vnet_app_worker_add_del (&args); |
| 1407 | if (rv) |
| 1408 | { |
| 1409 | clib_warning ("app worker add/del returned: %d", rv); |
| 1410 | goto done; |
| 1411 | } |
| 1412 | |
| 1413 | if (!mp->is_add) |
| 1414 | { |
| 1415 | sapi_socket_close_w_handle (sapi_handle); |
| 1416 | goto done; |
| 1417 | } |
| 1418 | |
| 1419 | /* Send fifo segment fd if needed */ |
| 1420 | if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) |
| 1421 | { |
| 1422 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 1423 | fds[n_fds] = args.segment->fd; |
| 1424 | n_fds += 1; |
| 1425 | } |
| 1426 | if (application_segment_manager_properties (app)->use_mq_eventfd) |
| 1427 | { |
| 1428 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
Florin Coras | 86f1232 | 2021-01-22 15:05:14 -0800 | [diff] [blame] | 1429 | fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1430 | n_fds += 1; |
| 1431 | } |
| 1432 | |
| 1433 | done: |
| 1434 | |
| 1435 | msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY; |
| 1436 | rmp = &msg.worker_add_del_reply; |
| 1437 | rmp->retval = rv; |
| 1438 | rmp->is_add = mp->is_add; |
Florin Coras | cc7c88e | 2020-09-15 15:56:51 -0700 | [diff] [blame] | 1439 | rmp->api_client_handle = sapi_handle; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1440 | rmp->wrk_index = args.wrk_map_index; |
| 1441 | rmp->segment_handle = args.segment_handle; |
| 1442 | if (!rv && mp->is_add) |
| 1443 | { |
| 1444 | /* No segment name and size. This supports only memfds */ |
Florin Coras | b462418 | 2020-12-11 13:58:12 -0800 | [diff] [blame] | 1445 | rmp->app_event_queue_address = |
| 1446 | fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1447 | rmp->n_fds = n_fds; |
| 1448 | rmp->fd_flags = fd_flags; |
| 1449 | |
| 1450 | /* Update app index for socket */ |
| 1451 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1452 | app_wrk = application_get_worker (app, args.wrk_map_index); |
| 1453 | handle->aah_app_wrk_index = app_wrk->wrk_index; |
| 1454 | } |
| 1455 | |
| 1456 | clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds); |
| 1457 | } |
| 1458 | |
| 1459 | static void |
| 1460 | sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs) |
| 1461 | { |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1462 | app_ns_api_handle_t *handle; |
| 1463 | app_worker_t *app_wrk; |
| 1464 | u32 api_client_handle; |
| 1465 | |
| 1466 | api_client_handle = appns_sapi_socket_handle (app_ns, cs); |
| 1467 | sapi_socket_close_w_handle (api_client_handle); |
| 1468 | |
Florin Coras | f99a7d6 | 2020-09-14 10:29:29 -0700 | [diff] [blame] | 1469 | /* Cleanup everything because app worker closed socket or crashed */ |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1470 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1471 | app_wrk = app_worker_get (handle->aah_app_wrk_index); |
Florin Coras | f99a7d6 | 2020-09-14 10:29:29 -0700 | [diff] [blame] | 1472 | |
| 1473 | vnet_app_worker_add_del_args_t args = { |
| 1474 | .app_index = app_wrk->app_index, |
| 1475 | .wrk_map_index = app_wrk->wrk_map_index, |
| 1476 | .api_client_index = api_client_handle, |
| 1477 | .is_add = 0 |
| 1478 | }; |
| 1479 | /* Send rpc to main thread for worker barrier */ |
| 1480 | vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args, |
| 1481 | sizeof (args)); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static clib_error_t * |
| 1485 | sapi_sock_read_ready (clib_file_t * cf) |
| 1486 | { |
| 1487 | app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data; |
Florin Coras | 7360e3d | 2020-09-18 16:15:47 -0700 | [diff] [blame] | 1488 | vlib_main_t *vm = vlib_get_main (); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1489 | app_sapi_msg_t msg = { 0 }; |
| 1490 | app_namespace_t *app_ns; |
| 1491 | clib_error_t *err = 0; |
| 1492 | clib_socket_t *cs; |
| 1493 | |
| 1494 | app_ns = app_namespace_get (handle->aah_app_ns_index); |
| 1495 | cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index); |
| 1496 | if (!cs) |
| 1497 | goto error; |
| 1498 | |
| 1499 | err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0); |
| 1500 | if (err) |
| 1501 | { |
| 1502 | clib_error_free (err); |
| 1503 | sapi_socket_detach (app_ns, cs); |
| 1504 | goto error; |
| 1505 | } |
| 1506 | |
| 1507 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1508 | |
Florin Coras | 7360e3d | 2020-09-18 16:15:47 -0700 | [diff] [blame] | 1509 | vlib_worker_thread_barrier_sync (vm); |
| 1510 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1511 | switch (msg.type) |
| 1512 | { |
| 1513 | case APP_SAPI_MSG_TYPE_ATTACH: |
| 1514 | session_api_attach_handler (app_ns, cs, &msg.attach); |
| 1515 | break; |
| 1516 | case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER: |
| 1517 | sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del); |
| 1518 | break; |
| 1519 | default: |
| 1520 | clib_warning ("app wrk %u unknown message type: %u", |
| 1521 | handle->aah_app_wrk_index, msg.type); |
| 1522 | break; |
| 1523 | } |
| 1524 | |
Florin Coras | 7360e3d | 2020-09-18 16:15:47 -0700 | [diff] [blame] | 1525 | vlib_worker_thread_barrier_release (vm); |
| 1526 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1527 | error: |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static clib_error_t * |
| 1532 | sapi_sock_write_ready (clib_file_t * cf) |
| 1533 | { |
| 1534 | app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data; |
| 1535 | clib_warning ("called for app ns %u", handle->aah_app_ns_index); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | static clib_error_t * |
| 1540 | sapi_sock_error (clib_file_t * cf) |
| 1541 | { |
| 1542 | app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data; |
| 1543 | app_namespace_t *app_ns; |
| 1544 | clib_socket_t *cs; |
| 1545 | |
| 1546 | app_ns = app_namespace_get (handle->aah_app_ns_index); |
| 1547 | cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index); |
| 1548 | if (!cs) |
| 1549 | return 0; |
| 1550 | |
| 1551 | sapi_socket_detach (app_ns, cs); |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | static clib_error_t * |
| 1556 | sapi_sock_accept_ready (clib_file_t * scf) |
| 1557 | { |
| 1558 | app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data; |
| 1559 | app_namespace_t *app_ns; |
| 1560 | clib_file_t cf = { 0 }; |
| 1561 | clib_error_t *err = 0; |
| 1562 | clib_socket_t *ccs, *scs; |
| 1563 | |
| 1564 | /* Listener files point to namespace */ |
| 1565 | app_ns = app_namespace_get (handle.aah_app_ns_index); |
| 1566 | |
| 1567 | /* |
| 1568 | * Initialize client socket |
| 1569 | */ |
| 1570 | ccs = appns_sapi_alloc_socket (app_ns); |
| 1571 | |
| 1572 | /* Grab server socket after client is initialized */ |
| 1573 | scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index); |
| 1574 | if (!scs) |
| 1575 | goto error; |
| 1576 | |
| 1577 | err = clib_socket_accept (scs, ccs); |
| 1578 | if (err) |
| 1579 | { |
| 1580 | clib_error_report (err); |
| 1581 | goto error; |
| 1582 | } |
| 1583 | |
| 1584 | cf.read_function = sapi_sock_read_ready; |
| 1585 | cf.write_function = sapi_sock_write_ready; |
| 1586 | cf.error_function = sapi_sock_error; |
| 1587 | cf.file_descriptor = ccs->fd; |
| 1588 | /* File points to app namespace and socket */ |
| 1589 | handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs); |
Florin Coras | 6b6c10b | 2020-09-24 11:58:28 -0700 | [diff] [blame] | 1590 | cf.private_data = handle.as_u64; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1591 | cf.description = format (0, "app sock conn fd: %d", ccs->fd); |
| 1592 | |
| 1593 | /* Poll until we get an attach message. Socket points to file and |
| 1594 | * application that owns the socket */ |
| 1595 | handle.aah_app_wrk_index = APP_INVALID_INDEX; |
| 1596 | handle.aah_file_index = clib_file_add (&file_main, &cf); |
Florin Coras | 6b6c10b | 2020-09-24 11:58:28 -0700 | [diff] [blame] | 1597 | ccs->private_data = handle.as_u64; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1598 | |
| 1599 | return err; |
| 1600 | |
| 1601 | error: |
| 1602 | appns_sapi_free_socket (app_ns, ccs); |
| 1603 | return err; |
| 1604 | } |
| 1605 | |
| 1606 | int |
| 1607 | appns_sapi_add_ns_socket (app_namespace_t * app_ns) |
| 1608 | { |
| 1609 | char *subdir = "/app_ns_sockets/"; |
| 1610 | app_ns_api_handle_t *handle; |
| 1611 | clib_file_t cf = { 0 }; |
| 1612 | struct stat file_stat; |
| 1613 | clib_error_t *err; |
| 1614 | clib_socket_t *cs; |
| 1615 | u8 *dir = 0; |
| 1616 | int rv = 0; |
| 1617 | |
| 1618 | vec_add (dir, vlib_unix_get_runtime_dir (), |
| 1619 | strlen (vlib_unix_get_runtime_dir ())); |
| 1620 | vec_add (dir, (u8 *) subdir, strlen (subdir)); |
| 1621 | |
| 1622 | err = vlib_unix_recursive_mkdir ((char *) dir); |
| 1623 | if (err) |
| 1624 | { |
| 1625 | clib_error_report (err); |
| 1626 | rv = -1; |
| 1627 | goto error; |
| 1628 | } |
| 1629 | |
| 1630 | app_ns->sock_name = format (0, "%v%v%c", dir, app_ns->ns_id, 0); |
| 1631 | |
| 1632 | /* |
| 1633 | * Create and initialize socket to listen on |
| 1634 | */ |
| 1635 | cs = appns_sapi_alloc_socket (app_ns); |
| 1636 | cs->config = (char *) app_ns->sock_name; |
| 1637 | cs->flags = CLIB_SOCKET_F_IS_SERVER | |
| 1638 | CLIB_SOCKET_F_ALLOW_GROUP_WRITE | |
| 1639 | CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED; |
| 1640 | |
| 1641 | if ((err = clib_socket_init (cs))) |
| 1642 | { |
| 1643 | clib_error_report (err); |
| 1644 | rv = -1; |
| 1645 | goto error; |
| 1646 | } |
| 1647 | |
| 1648 | if (stat ((char *) app_ns->sock_name, &file_stat) == -1) |
| 1649 | { |
| 1650 | rv = -1; |
| 1651 | goto error; |
| 1652 | } |
| 1653 | |
| 1654 | /* |
| 1655 | * Start polling it |
| 1656 | */ |
| 1657 | cf.read_function = sapi_sock_accept_ready; |
| 1658 | cf.file_descriptor = cs->fd; |
| 1659 | /* File points to namespace */ |
| 1660 | handle = (app_ns_api_handle_t *) & cf.private_data; |
| 1661 | handle->aah_app_ns_index = app_namespace_index (app_ns); |
| 1662 | handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs); |
| 1663 | cf.description = format (0, "app sock listener: %s", app_ns->sock_name); |
| 1664 | |
| 1665 | /* Socket points to clib file index */ |
| 1666 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1667 | handle->aah_file_index = clib_file_add (&file_main, &cf); |
| 1668 | handle->aah_app_wrk_index = APP_INVALID_INDEX; |
| 1669 | |
| 1670 | error: |
| 1671 | vec_free (dir); |
| 1672 | return rv; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1676 | * fd.io coding-style-patch-verification: ON |
| 1677 | * |
| 1678 | * Local Variables: |
| 1679 | * eval: (c-set-style "gnu") |
| 1680 | * End: |
| 1681 | */ |