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