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