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