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