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