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