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 | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 183 | vpp_queue = session_main_get_vpp_event_queue (0); |
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 | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 321 | vpp_mq = session_main_get_vpp_event_queue (0); |
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 | } |
| 617 | |
| 618 | STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <= |
| 619 | sizeof (mp->options), |
| 620 | "Out of options, fix api message definition"); |
| 621 | |
| 622 | clib_memset (a, 0, sizeof (*a)); |
| 623 | a->api_client_index = mp->client_index; |
| 624 | a->options = mp->options; |
| 625 | a->session_cb_vft = &session_mq_cb_vft; |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 626 | |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 627 | 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] | 628 | |
| 629 | if ((rv = vnet_application_attach (a))) |
| 630 | { |
| 631 | clib_warning ("attach returned: %d", rv); |
| 632 | vec_free (a->namespace_id); |
| 633 | goto done; |
| 634 | } |
| 635 | vec_free (a->namespace_id); |
| 636 | |
| 637 | /* Send event queues segment */ |
| 638 | if ((evt_q_segment = session_main_get_evt_q_segment ())) |
| 639 | { |
| 640 | fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT; |
| 641 | fds[n_fds] = evt_q_segment->fd; |
| 642 | n_fds += 1; |
| 643 | } |
| 644 | /* Send fifo segment fd if needed */ |
| 645 | if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) |
| 646 | { |
| 647 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 648 | fds[n_fds] = a->segment->fd; |
| 649 | n_fds += 1; |
| 650 | } |
| 651 | if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 652 | { |
| 653 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
| 654 | fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q); |
| 655 | n_fds += 1; |
| 656 | } |
| 657 | |
| 658 | done: |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 659 | /* *INDENT-OFF* */ |
| 660 | REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({ |
| 661 | if (!rv) |
| 662 | { |
Vratko Polak | 4f59985 | 2019-11-08 10:32:39 +0100 | [diff] [blame] | 663 | ctrl_thread = vlib_num_workers () ? 1 : 0; |
Nathan Skrzypczak | 9d3e1b4 | 2019-11-07 10:29:24 +0100 | [diff] [blame] | 664 | ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 665 | segp = a->segment; |
| 666 | rmp->app_index = clib_host_to_net_u32 (a->app_index); |
| 667 | rmp->app_mq = pointer_to_uword (a->app_evt_q); |
| 668 | rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq); |
| 669 | rmp->vpp_ctrl_mq_thread = ctrl_thread; |
| 670 | rmp->n_fds = n_fds; |
| 671 | rmp->fd_flags = fd_flags; |
| 672 | if (vec_len (segp->name)) |
| 673 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 674 | vl_api_vec_to_api_string (segp->name, &rmp->segment_name); |
Florin Coras | 458089b | 2019-08-21 16:20:44 -0700 | [diff] [blame] | 675 | } |
| 676 | rmp->segment_size = segp->ssvm_size; |
| 677 | rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle); |
| 678 | } |
| 679 | })); |
| 680 | /* *INDENT-ON* */ |
| 681 | |
| 682 | if (n_fds) |
| 683 | session_send_fds (reg, fds, n_fds); |
| 684 | } |
| 685 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 686 | static void |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 687 | vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp) |
| 688 | { |
| 689 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 690 | vl_api_app_worker_add_del_reply_t *rmp; |
| 691 | vl_api_registration_t *reg; |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 692 | application_t *app; |
| 693 | u8 fd_flags = 0; |
| 694 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 695 | if (!session_main_is_enabled () || appns_sapi_enabled ()) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 696 | { |
| 697 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 698 | goto done; |
| 699 | } |
| 700 | |
| 701 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 702 | if (!reg) |
| 703 | return; |
| 704 | |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 705 | 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] | 706 | if (!app) |
| 707 | { |
| 708 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 709 | goto done; |
| 710 | } |
| 711 | |
| 712 | vnet_app_worker_add_del_args_t args = { |
| 713 | .app_index = app->app_index, |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 714 | .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index), |
Florin Coras | c1f5a43 | 2018-11-20 11:31:26 -0800 | [diff] [blame] | 715 | .api_client_index = mp->client_index, |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 716 | .is_add = mp->is_add |
| 717 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 718 | rv = vnet_app_worker_add_del (&args); |
| 719 | if (rv) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 720 | { |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 721 | clib_warning ("app worker add/del returned: %d", rv); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 722 | goto done; |
| 723 | } |
| 724 | |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 725 | if (!mp->is_add) |
| 726 | goto done; |
Florin Coras | c3638fe | 2018-08-24 13:58:49 -0700 | [diff] [blame] | 727 | |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 728 | /* Send fifo segment fd if needed */ |
| 729 | if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) |
| 730 | { |
| 731 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 732 | fds[n_fds] = args.segment->fd; |
| 733 | n_fds += 1; |
| 734 | } |
| 735 | if (application_segment_manager_properties (app)->use_mq_eventfd) |
| 736 | { |
| 737 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
| 738 | fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q); |
| 739 | n_fds += 1; |
| 740 | } |
| 741 | |
| 742 | /* *INDENT-OFF* */ |
| 743 | done: |
| 744 | REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({ |
| 745 | rmp->is_add = mp->is_add; |
Florin Coras | 349f8ca | 2018-11-20 16:52:49 -0800 | [diff] [blame] | 746 | rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index); |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 747 | rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle); |
Florin Coras | 1459877 | 2018-09-04 19:47:52 -0700 | [diff] [blame] | 748 | if (!rv && mp->is_add) |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 749 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 750 | if (vec_len (args.segment->name)) |
| 751 | { |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 752 | vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name); |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 753 | } |
| 754 | rmp->app_event_queue_address = pointer_to_uword (args.evt_q); |
| 755 | rmp->n_fds = n_fds; |
| 756 | rmp->fd_flags = fd_flags; |
| 757 | } |
| 758 | })); |
| 759 | /* *INDENT-ON* */ |
| 760 | |
| 761 | if (n_fds) |
| 762 | session_send_fds (reg, fds, n_fds); |
| 763 | } |
| 764 | |
| 765 | static void |
Florin Coras | 888d9f0 | 2020-04-02 23:00:13 +0000 | [diff] [blame] | 766 | vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) |
| 767 | { |
| 768 | vl_api_application_detach_reply_t *rmp; |
| 769 | int rv = VNET_API_ERROR_INVALID_VALUE_2; |
| 770 | vnet_app_detach_args_t _a, *a = &_a; |
| 771 | application_t *app; |
| 772 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 773 | if (!session_main_is_enabled () || appns_sapi_enabled ()) |
Florin Coras | 888d9f0 | 2020-04-02 23:00:13 +0000 | [diff] [blame] | 774 | { |
| 775 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 776 | goto done; |
| 777 | } |
| 778 | |
| 779 | app = application_lookup (mp->client_index); |
| 780 | if (app) |
| 781 | { |
| 782 | a->app_index = app->app_index; |
| 783 | a->api_client_index = mp->client_index; |
| 784 | rv = vnet_application_detach (a); |
| 785 | } |
| 786 | |
| 787 | done: |
| 788 | REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); |
| 789 | } |
| 790 | |
| 791 | static void |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 792 | vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) |
| 793 | { |
| 794 | vl_api_app_namespace_add_del_reply_t *rmp; |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 795 | u32 appns_index = 0; |
| 796 | u8 *ns_id = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 797 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 798 | if (session_main_is_enabled () == 0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 799 | { |
| 800 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 801 | goto done; |
| 802 | } |
| 803 | |
Dave Barach | 7784140 | 2020-04-29 17:04:10 -0400 | [diff] [blame] | 804 | 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] | 805 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 806 | vnet_app_namespace_add_del_args_t args = { |
| 807 | .ns_id = ns_id, |
Florin Coras | 9a9adb2 | 2017-10-26 08:16:59 -0700 | [diff] [blame] | 808 | .secret = clib_net_to_host_u64 (mp->secret), |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 809 | .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index), |
| 810 | .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id), |
| 811 | .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id), |
| 812 | .is_add = 1 |
| 813 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 814 | rv = vnet_app_namespace_add_del (&args); |
| 815 | if (!rv) |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 816 | { |
| 817 | appns_index = app_namespace_index_from_id (ns_id); |
| 818 | if (appns_index == APP_NAMESPACE_INVALID_INDEX) |
| 819 | { |
| 820 | clib_warning ("app ns lookup failed"); |
| 821 | rv = VNET_API_ERROR_UNSPECIFIED; |
| 822 | } |
| 823 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 824 | vec_free (ns_id); |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 825 | |
| 826 | /* *INDENT-OFF* */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 827 | done: |
Florin Coras | 6e8c667 | 2017-11-10 09:03:54 -0800 | [diff] [blame] | 828 | REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({ |
| 829 | if (!rv) |
| 830 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
| 831 | })); |
| 832 | /* *INDENT-ON* */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 835 | static void |
| 836 | vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp) |
| 837 | { |
| 838 | vl_api_session_rule_add_del_reply_t *rmp; |
| 839 | session_rule_add_del_args_t args; |
| 840 | session_rule_table_add_del_args_t *table_args = &args.table_args; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 841 | int rv = 0; |
| 842 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 843 | clib_memset (&args, 0, sizeof (args)); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 844 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 845 | ip_prefix_decode (&mp->lcl, &table_args->lcl); |
| 846 | ip_prefix_decode (&mp->rmt, &table_args->rmt); |
| 847 | |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 848 | table_args->lcl_port = mp->lcl_port; |
| 849 | table_args->rmt_port = mp->rmt_port; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 850 | table_args->action_index = clib_net_to_host_u32 (mp->action_index); |
| 851 | table_args->is_add = mp->is_add; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 852 | mp->tag[sizeof (mp->tag) - 1] = 0; |
| 853 | table_args->tag = format (0, "%s", mp->tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 854 | args.appns_index = clib_net_to_host_u32 (mp->appns_index); |
| 855 | args.scope = mp->scope; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 856 | args.transport_proto = |
| 857 | api_session_transport_proto_decode (&mp->transport_proto) == |
| 858 | TRANSPORT_PROTO_UDP ? 1 : 0; |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 859 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 860 | rv = vnet_session_rule_add_del (&args); |
| 861 | if (rv) |
| 862 | clib_warning ("rule add del returned: %d", rv); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 863 | vec_free (table_args->tag); |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 864 | REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY); |
| 865 | } |
| 866 | |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 867 | static void |
| 868 | send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 869 | u8 transport_proto, u32 appns_index, u8 * tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 870 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 871 | { |
| 872 | vl_api_session_rules_details_t *rmp = 0; |
| 873 | session_mask_or_match_4_t *match = |
| 874 | (session_mask_or_match_4_t *) & rule->match; |
| 875 | session_mask_or_match_4_t *mask = |
| 876 | (session_mask_or_match_4_t *) & rule->mask; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 877 | fib_prefix_t lcl, rmt; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 878 | |
| 879 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 880 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 881 | rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); |
| 882 | rmp->context = context; |
| 883 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 884 | clib_memset (&lcl, 0, sizeof (lcl)); |
| 885 | clib_memset (&rmt, 0, sizeof (rmt)); |
| 886 | ip_set (&lcl.fp_addr, &match->lcl_ip, 1); |
| 887 | ip_set (&rmt.fp_addr, &match->rmt_ip, 1); |
| 888 | lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip); |
| 889 | rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip); |
| 890 | |
| 891 | ip_prefix_encode (&lcl, &rmp->lcl); |
| 892 | ip_prefix_encode (&rmt, &rmp->rmt); |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 893 | rmp->lcl_port = match->lcl_port; |
| 894 | rmp->rmt_port = match->rmt_port; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 895 | rmp->action_index = clib_host_to_net_u32 (rule->action_index); |
| 896 | rmp->scope = |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 897 | is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL; |
| 898 | rmp->transport_proto = api_session_transport_proto_encode (transport_proto); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 899 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 900 | if (tag) |
| 901 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 902 | clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 903 | rmp->tag[vec_len (tag)] = 0; |
| 904 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 905 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 906 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static void |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 910 | send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local, |
| 911 | u8 transport_proto, u32 appns_index, u8 * tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 912 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 913 | { |
| 914 | vl_api_session_rules_details_t *rmp = 0; |
| 915 | session_mask_or_match_6_t *match = |
| 916 | (session_mask_or_match_6_t *) & rule->match; |
| 917 | session_mask_or_match_6_t *mask = |
| 918 | (session_mask_or_match_6_t *) & rule->mask; |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 919 | fib_prefix_t lcl, rmt; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 920 | |
| 921 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 922 | clib_memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 923 | rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); |
| 924 | rmp->context = context; |
| 925 | |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 926 | clib_memset (&lcl, 0, sizeof (lcl)); |
| 927 | clib_memset (&rmt, 0, sizeof (rmt)); |
| 928 | ip_set (&lcl.fp_addr, &match->lcl_ip, 0); |
| 929 | ip_set (&rmt.fp_addr, &match->rmt_ip, 0); |
| 930 | lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip); |
| 931 | rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip); |
| 932 | |
| 933 | ip_prefix_encode (&lcl, &rmp->lcl); |
| 934 | ip_prefix_encode (&rmt, &rmp->rmt); |
Milan Lenco | 8b9a5d1 | 2017-11-24 17:12:33 +0100 | [diff] [blame] | 935 | rmp->lcl_port = match->lcl_port; |
| 936 | rmp->rmt_port = match->rmt_port; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 937 | rmp->action_index = clib_host_to_net_u32 (rule->action_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 938 | rmp->scope = |
Jakub Grajciar | b4e5e50 | 2020-01-31 09:35:29 +0100 | [diff] [blame] | 939 | is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL; |
| 940 | rmp->transport_proto = api_session_transport_proto_encode (transport_proto); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 941 | rmp->appns_index = clib_host_to_net_u32 (appns_index); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 942 | if (tag) |
| 943 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 944 | clib_memcpy_fast (rmp->tag, tag, vec_len (tag)); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 945 | rmp->tag[vec_len (tag)] = 0; |
| 946 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 947 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 948 | vl_api_send_msg (reg, (u8 *) rmp); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static void |
| 952 | 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] | 953 | u8 tp, u8 is_local, u32 appns_index, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 954 | vl_api_registration_t * reg, u32 context) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 955 | { |
| 956 | mma_rule_16_t *rule16; |
| 957 | mma_rule_40_t *rule40; |
| 958 | mma_rules_table_16_t *srt16; |
| 959 | mma_rules_table_40_t *srt40; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 960 | u32 ri; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 961 | |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 962 | if (is_local || fib_proto == FIB_PROTOCOL_IP4) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 963 | { |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 964 | u8 *tag = 0; |
| 965 | /* *INDENT-OFF* */ |
| 966 | srt16 = &srt->session_rules_tables_16; |
| 967 | pool_foreach (rule16, srt16->rules, ({ |
| 968 | ri = mma_rules_table_rule_index_16 (srt16, rule16); |
| 969 | tag = session_rules_table_rule_tag (srt, ri, 1); |
| 970 | send_session_rule_details4 (rule16, is_local, tp, appns_index, tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 971 | reg, context); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 972 | })); |
| 973 | /* *INDENT-ON* */ |
| 974 | } |
| 975 | if (is_local || fib_proto == FIB_PROTOCOL_IP6) |
| 976 | { |
| 977 | u8 *tag = 0; |
| 978 | /* *INDENT-OFF* */ |
| 979 | srt40 = &srt->session_rules_tables_40; |
| 980 | pool_foreach (rule40, srt40->rules, ({ |
| 981 | ri = mma_rules_table_rule_index_40 (srt40, rule40); |
| 982 | tag = session_rules_table_rule_tag (srt, ri, 1); |
| 983 | send_session_rule_details6 (rule40, is_local, tp, appns_index, tag, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 984 | reg, context); |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 985 | })); |
| 986 | /* *INDENT-ON* */ |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | static void |
| 991 | vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp) |
| 992 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 993 | vl_api_registration_t *reg; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 994 | session_table_t *st; |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 995 | u8 tp; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 996 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 997 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 998 | if (!reg) |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 999 | return; |
| 1000 | |
| 1001 | /* *INDENT-OFF* */ |
| 1002 | session_table_foreach (st, ({ |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 1003 | for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++) |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1004 | { |
| 1005 | send_session_rules_table_details (&st->session_rules[tp], |
| 1006 | st->active_fib_proto, tp, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 1007 | st->is_local, st->appns_index, reg, |
Florin Coras | c97a739 | 2017-11-05 23:07:07 -0800 | [diff] [blame] | 1008 | mp->context); |
| 1009 | } |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 1010 | })); |
| 1011 | /* *INDENT-ON* */ |
| 1012 | } |
| 1013 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1014 | static void |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1015 | 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] | 1016 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1017 | vl_api_app_add_cert_key_pair_reply_t *rmp; |
| 1018 | vnet_app_add_cert_key_pair_args_t _a, *a = &_a; |
| 1019 | u32 certkey_len, key_len, cert_len; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1020 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1021 | if (session_main_is_enabled () == 0) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1022 | { |
| 1023 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1024 | goto done; |
| 1025 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1026 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1027 | cert_len = clib_net_to_host_u16 (mp->cert_len); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 1028 | if (cert_len > 10000) |
| 1029 | { |
| 1030 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1031 | goto done; |
| 1032 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1033 | |
| 1034 | certkey_len = clib_net_to_host_u16 (mp->certkey_len); |
| 1035 | if (certkey_len < cert_len) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1036 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1037 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1038 | goto done; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1039 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1040 | |
| 1041 | key_len = certkey_len - cert_len; |
| 1042 | if (key_len > 10000) |
| 1043 | { |
| 1044 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1045 | goto done; |
| 1046 | } |
| 1047 | |
| 1048 | clib_memset (a, 0, sizeof (*a)); |
| 1049 | vec_validate (a->cert, cert_len); |
| 1050 | vec_validate (a->key, key_len); |
| 1051 | clib_memcpy_fast (a->cert, mp->certkey, cert_len); |
| 1052 | clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len); |
| 1053 | rv = vnet_app_add_cert_key_pair (a); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1054 | vec_free (a->cert); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1055 | vec_free (a->key); |
| 1056 | |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1057 | done: |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1058 | /* *INDENT-OFF* */ |
| 1059 | REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({ |
| 1060 | if (!rv) |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1061 | rmp->index = clib_host_to_net_u32 (a->index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1062 | })); |
| 1063 | /* *INDENT-ON* */ |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | static void |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1067 | 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] | 1068 | { |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1069 | vl_api_app_del_cert_key_pair_reply_t *rmp; |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1070 | u32 ckpair_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1071 | int rv = 0; |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1072 | if (session_main_is_enabled () == 0) |
| 1073 | { |
| 1074 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1075 | goto done; |
| 1076 | } |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1077 | ckpair_index = clib_net_to_host_u32 (mp->index); |
| 1078 | rv = vnet_app_del_cert_key_pair (ckpair_index); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1079 | |
| 1080 | done: |
Nathan Skrzypczak | 45ec9f4 | 2019-11-06 14:47:40 +0100 | [diff] [blame] | 1081 | REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY); |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* ### WILL BE DEPRECATED POST 20.01 ### */ |
| 1085 | static void |
| 1086 | vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t * |
| 1087 | mp) |
| 1088 | { |
| 1089 | vl_api_application_tls_cert_add_reply_t *rmp; |
| 1090 | app_cert_key_pair_t *ckpair; |
| 1091 | application_t *app; |
| 1092 | u32 cert_len; |
| 1093 | int rv = 0; |
| 1094 | if (session_main_is_enabled () == 0) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1095 | { |
| 1096 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1097 | goto done; |
| 1098 | } |
Florin Coras | e3e2f07 | 2018-03-04 07:24:30 -0800 | [diff] [blame] | 1099 | if (!(app = application_lookup (mp->client_index))) |
| 1100 | { |
| 1101 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 1102 | goto done; |
| 1103 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1104 | cert_len = clib_net_to_host_u16 (mp->cert_len); |
| 1105 | if (cert_len > 10000) |
| 1106 | { |
| 1107 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1108 | goto done; |
| 1109 | } |
| 1110 | ckpair = app_cert_key_pair_get_default (); |
| 1111 | vec_validate (ckpair->cert, cert_len); |
| 1112 | clib_memcpy_fast (ckpair->cert, mp->cert, cert_len); |
| 1113 | |
| 1114 | done: |
| 1115 | REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY); |
| 1116 | } |
| 1117 | |
| 1118 | /* ### WILL BE DEPRECATED POST 20.01 ### */ |
| 1119 | static void |
| 1120 | vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t * |
| 1121 | mp) |
| 1122 | { |
| 1123 | vl_api_application_tls_key_add_reply_t *rmp; |
| 1124 | app_cert_key_pair_t *ckpair; |
| 1125 | application_t *app; |
| 1126 | u32 key_len; |
| 1127 | int rv = 0; |
| 1128 | if (session_main_is_enabled () == 0) |
| 1129 | { |
| 1130 | rv = VNET_API_ERROR_FEATURE_DISABLED; |
| 1131 | goto done; |
| 1132 | } |
| 1133 | if (!(app = application_lookup (mp->client_index))) |
| 1134 | { |
| 1135 | rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; |
| 1136 | goto done; |
| 1137 | } |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1138 | key_len = clib_net_to_host_u16 (mp->key_len); |
Florin Coras | 5090c57 | 2018-03-18 08:22:17 -0700 | [diff] [blame] | 1139 | if (key_len > 10000) |
| 1140 | { |
| 1141 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1142 | goto done; |
| 1143 | } |
Nathan Skrzypczak | 79f8953 | 2019-09-13 11:08:13 +0200 | [diff] [blame] | 1144 | ckpair = app_cert_key_pair_get_default (); |
| 1145 | vec_validate (ckpair->key, key_len); |
| 1146 | clib_memcpy_fast (ckpair->key, mp->key, key_len); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 1147 | done: |
| 1148 | REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY); |
| 1149 | } |
| 1150 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1151 | static clib_error_t * |
| 1152 | application_reaper_cb (u32 client_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1153 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1154 | application_t *app = application_lookup (client_index); |
| 1155 | vnet_app_detach_args_t _a, *a = &_a; |
| 1156 | if (app) |
| 1157 | { |
Florin Coras | 1553197 | 2018-08-12 23:50:53 -0700 | [diff] [blame] | 1158 | a->app_index = app->app_index; |
Florin Coras | 053a0e4 | 2018-11-13 15:52:38 -0800 | [diff] [blame] | 1159 | a->api_client_index = client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1160 | vnet_application_detach (a); |
| 1161 | } |
| 1162 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1163 | } |
| 1164 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1165 | VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1166 | |
| 1167 | #define vl_msg_name_crc_list |
| 1168 | #include <vnet/vnet_all_api_h.h> |
| 1169 | #undef vl_msg_name_crc_list |
| 1170 | |
| 1171 | static void |
| 1172 | setup_message_id_table (api_main_t * am) |
| 1173 | { |
| 1174 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 1175 | foreach_vl_msg_name_crc_session; |
| 1176 | #undef _ |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * session_api_hookup |
| 1181 | * Add uri's API message handlers to the table. |
Jim Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 1182 | * vlib has already mapped shared memory and |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1183 | * added the client registration handlers. |
| 1184 | * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() |
| 1185 | */ |
| 1186 | static clib_error_t * |
| 1187 | session_api_hookup (vlib_main_t * vm) |
| 1188 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 1189 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1190 | |
| 1191 | #define _(N,n) \ |
| 1192 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1193 | vl_api_##n##_t_handler, \ |
| 1194 | vl_noop_handler, \ |
| 1195 | vl_api_##n##_t_endian, \ |
| 1196 | vl_api_##n##_t_print, \ |
| 1197 | sizeof(vl_api_##n##_t), 1); |
| 1198 | foreach_session_api_msg; |
| 1199 | #undef _ |
| 1200 | |
| 1201 | /* |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1202 | * Set up the (msg_name, crc, message-id) table |
| 1203 | */ |
| 1204 | setup_message_id_table (am); |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | VLIB_API_INIT_FUNCTION (session_api_hookup); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1210 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1211 | /* |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1212 | * Socket api functions |
| 1213 | */ |
| 1214 | |
| 1215 | static void |
| 1216 | sapi_send_fds (app_worker_t * app_wrk, int *fds, int n_fds) |
| 1217 | { |
| 1218 | app_sapi_msg_t smsg = { 0 }; |
| 1219 | app_namespace_t *app_ns; |
| 1220 | application_t *app; |
| 1221 | clib_socket_t *cs; |
| 1222 | u32 cs_index; |
| 1223 | |
| 1224 | app = application_get (app_wrk->app_index); |
| 1225 | app_ns = app_namespace_get (app->ns_index); |
| 1226 | cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index); |
| 1227 | cs = appns_sapi_get_socket (app_ns, cs_index); |
| 1228 | |
| 1229 | /* There's no payload for the message only the type */ |
| 1230 | smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS; |
| 1231 | clib_socket_sendmsg (cs, &smsg, sizeof (smsg), fds, n_fds); |
| 1232 | } |
| 1233 | |
| 1234 | static int |
| 1235 | mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle) |
| 1236 | { |
| 1237 | int fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 1238 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 1239 | session_app_add_segment_msg_t *mp; |
| 1240 | app_worker_t *app_wrk; |
| 1241 | session_event_t *evt; |
| 1242 | svm_msg_q_t *app_mq; |
| 1243 | fifo_segment_t *fs; |
| 1244 | ssvm_private_t *sp; |
| 1245 | u8 fd_flags = 0; |
| 1246 | |
| 1247 | app_wrk = app_worker_get (app_wrk_index); |
| 1248 | |
| 1249 | fs = segment_manager_get_segment_w_handle (segment_handle); |
| 1250 | sp = &fs->ssvm; |
| 1251 | ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD); |
| 1252 | |
| 1253 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 1254 | fds[n_fds] = sp->fd; |
| 1255 | n_fds += 1; |
| 1256 | |
| 1257 | app_mq = app_wrk->event_queue; |
| 1258 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 1259 | return -1; |
| 1260 | |
| 1261 | /* |
| 1262 | * Send the fd over api socket |
| 1263 | */ |
| 1264 | sapi_send_fds (app_wrk, fds, n_fds); |
| 1265 | |
| 1266 | /* |
| 1267 | * Send the actual message over mq |
| 1268 | */ |
| 1269 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 1270 | clib_memset (evt, 0, sizeof (*evt)); |
| 1271 | evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT; |
| 1272 | mp = (session_app_add_segment_msg_t *) evt->data; |
| 1273 | clib_memset (mp, 0, sizeof (*mp)); |
| 1274 | mp->segment_size = sp->ssvm_size; |
| 1275 | mp->fd_flags = fd_flags; |
| 1276 | mp->segment_handle = segment_handle; |
| 1277 | strncpy ((char *) mp->segment_name, (char *) sp->name, |
| 1278 | sizeof (mp->segment_name) - 1); |
| 1279 | |
| 1280 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static int |
| 1286 | mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle) |
| 1287 | { |
| 1288 | svm_msg_q_msg_t _msg, *msg = &_msg; |
| 1289 | session_app_del_segment_msg_t *mp; |
| 1290 | app_worker_t *app_wrk; |
| 1291 | session_event_t *evt; |
| 1292 | svm_msg_q_t *app_mq; |
| 1293 | |
| 1294 | app_wrk = app_worker_get (app_wrk_index); |
| 1295 | |
| 1296 | app_mq = app_wrk->event_queue; |
| 1297 | if (mq_try_lock_and_alloc_msg (app_mq, msg)) |
| 1298 | return -1; |
| 1299 | |
| 1300 | evt = svm_msg_q_msg_data (app_mq, msg); |
| 1301 | clib_memset (evt, 0, sizeof (*evt)); |
| 1302 | evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT; |
| 1303 | mp = (session_app_del_segment_msg_t *) evt->data; |
| 1304 | clib_memset (mp, 0, sizeof (*mp)); |
| 1305 | mp->segment_handle = segment_handle; |
| 1306 | svm_msg_q_add_and_unlock (app_mq, msg); |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static session_cb_vft_t session_mq_sapi_cb_vft = { |
| 1312 | .session_accept_callback = mq_send_session_accepted_cb, |
| 1313 | .session_disconnect_callback = mq_send_session_disconnected_cb, |
| 1314 | .session_connected_callback = mq_send_session_connected_cb, |
| 1315 | .session_reset_callback = mq_send_session_reset_cb, |
| 1316 | .session_migrate_callback = mq_send_session_migrate_cb, |
| 1317 | .session_cleanup_callback = mq_send_session_cleanup_cb, |
| 1318 | .add_segment_callback = mq_send_add_segment_sapi_cb, |
| 1319 | .del_segment_callback = mq_send_del_segment_sapi_cb, |
| 1320 | }; |
| 1321 | |
| 1322 | static void |
| 1323 | session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs, |
| 1324 | app_sapi_attach_msg_t * mp) |
| 1325 | { |
| 1326 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 1327 | vnet_app_attach_args_t _a, *a = &_a; |
| 1328 | app_sapi_attach_reply_msg_t *rmp; |
| 1329 | ssvm_private_t *evt_q_segment; |
| 1330 | u8 fd_flags = 0, ctrl_thread; |
| 1331 | app_ns_api_handle_t *handle; |
| 1332 | app_sapi_msg_t msg = { 0 }; |
| 1333 | app_worker_t *app_wrk; |
| 1334 | svm_msg_q_t *ctrl_mq; |
| 1335 | application_t *app; |
| 1336 | |
| 1337 | /* Make sure name is null terminated */ |
| 1338 | mp->name[63] = 0; |
| 1339 | |
| 1340 | clib_memset (a, 0, sizeof (*a)); |
| 1341 | a->api_client_index = appns_sapi_socket_handle (app_ns, cs); |
| 1342 | a->name = format (0, "%s", (char *) mp->name); |
| 1343 | a->options = mp->options; |
| 1344 | a->session_cb_vft = &session_mq_sapi_cb_vft; |
| 1345 | a->use_sock_api = 1; |
| 1346 | a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns); |
| 1347 | |
| 1348 | if ((rv = vnet_application_attach (a))) |
| 1349 | { |
| 1350 | clib_warning ("attach returned: %d", rv); |
| 1351 | goto done; |
| 1352 | } |
| 1353 | |
| 1354 | /* Send event queues segment */ |
| 1355 | if ((evt_q_segment = session_main_get_evt_q_segment ())) |
| 1356 | { |
| 1357 | fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT; |
| 1358 | fds[n_fds] = evt_q_segment->fd; |
| 1359 | n_fds += 1; |
| 1360 | } |
| 1361 | /* Send fifo segment fd if needed */ |
| 1362 | if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) |
| 1363 | { |
| 1364 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 1365 | fds[n_fds] = a->segment->fd; |
| 1366 | n_fds += 1; |
| 1367 | } |
| 1368 | if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD) |
| 1369 | { |
| 1370 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
| 1371 | fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q); |
| 1372 | n_fds += 1; |
| 1373 | } |
| 1374 | |
| 1375 | done: |
| 1376 | |
| 1377 | msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY; |
| 1378 | rmp = &msg.attach_reply; |
| 1379 | rmp->retval = rv; |
| 1380 | if (!rv) |
| 1381 | { |
| 1382 | ctrl_thread = vlib_num_workers ()? 1 : 0; |
| 1383 | ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread); |
| 1384 | rmp->app_index = a->app_index; |
| 1385 | rmp->app_mq = pointer_to_uword (a->app_evt_q); |
| 1386 | rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq); |
| 1387 | rmp->vpp_ctrl_mq_thread = ctrl_thread; |
| 1388 | rmp->n_fds = n_fds; |
| 1389 | rmp->fd_flags = fd_flags; |
| 1390 | /* No segment name and size since we only support memfds |
| 1391 | * in this configuration */ |
| 1392 | rmp->segment_handle = a->segment_handle; |
| 1393 | rmp->api_client_handle = a->api_client_index; |
| 1394 | |
| 1395 | /* Update app index for socket */ |
| 1396 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1397 | app = application_get (a->app_index); |
| 1398 | app_wrk = application_get_worker (app, 0); |
| 1399 | handle->aah_app_wrk_index = app_wrk->wrk_index; |
| 1400 | } |
| 1401 | |
| 1402 | clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds); |
| 1403 | vec_free (a->name); |
| 1404 | } |
| 1405 | |
| 1406 | static void |
| 1407 | sapi_socket_close_w_handle (u32 api_handle) |
| 1408 | { |
| 1409 | app_namespace_t *app_ns = app_namespace_get (api_handle >> 16); |
| 1410 | u16 sock_index = api_handle & 0xffff; |
| 1411 | app_ns_api_handle_t *handle; |
| 1412 | clib_socket_t *cs; |
| 1413 | clib_file_t *cf; |
| 1414 | |
| 1415 | cs = appns_sapi_get_socket (app_ns, sock_index); |
| 1416 | if (!cs) |
| 1417 | return; |
| 1418 | |
| 1419 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1420 | cf = clib_file_get (&file_main, handle->aah_file_index); |
| 1421 | clib_file_del (&file_main, cf); |
| 1422 | |
| 1423 | clib_socket_close (cs); |
| 1424 | appns_sapi_free_socket (app_ns, cs); |
| 1425 | } |
| 1426 | |
| 1427 | static void |
| 1428 | sapi_add_del_worker_handler (app_namespace_t * app_ns, |
| 1429 | clib_socket_t * cs, |
| 1430 | app_sapi_worker_add_del_msg_t * mp) |
| 1431 | { |
| 1432 | int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0; |
| 1433 | app_sapi_worker_add_del_reply_msg_t *rmp; |
| 1434 | app_ns_api_handle_t *handle; |
| 1435 | app_sapi_msg_t msg = { 0 }; |
| 1436 | app_worker_t *app_wrk; |
| 1437 | application_t *app; |
| 1438 | u32 sapi_handle; |
| 1439 | u8 fd_flags = 0; |
| 1440 | |
| 1441 | app = application_get_if_valid (mp->app_index); |
| 1442 | if (!app) |
| 1443 | { |
| 1444 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 1445 | goto done; |
| 1446 | } |
| 1447 | |
| 1448 | sapi_handle = appns_sapi_socket_handle (app_ns, cs); |
| 1449 | |
| 1450 | vnet_app_worker_add_del_args_t args = { |
| 1451 | .app_index = app->app_index, |
| 1452 | .wrk_map_index = mp->wrk_index, |
| 1453 | .api_client_index = sapi_handle, |
| 1454 | .is_add = mp->is_add |
| 1455 | }; |
| 1456 | rv = vnet_app_worker_add_del (&args); |
| 1457 | if (rv) |
| 1458 | { |
| 1459 | clib_warning ("app worker add/del returned: %d", rv); |
| 1460 | goto done; |
| 1461 | } |
| 1462 | |
| 1463 | if (!mp->is_add) |
| 1464 | { |
| 1465 | sapi_socket_close_w_handle (sapi_handle); |
| 1466 | goto done; |
| 1467 | } |
| 1468 | |
| 1469 | /* Send fifo segment fd if needed */ |
| 1470 | if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD) |
| 1471 | { |
| 1472 | fd_flags |= SESSION_FD_F_MEMFD_SEGMENT; |
| 1473 | fds[n_fds] = args.segment->fd; |
| 1474 | n_fds += 1; |
| 1475 | } |
| 1476 | if (application_segment_manager_properties (app)->use_mq_eventfd) |
| 1477 | { |
| 1478 | fd_flags |= SESSION_FD_F_MQ_EVENTFD; |
| 1479 | fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q); |
| 1480 | n_fds += 1; |
| 1481 | } |
| 1482 | |
| 1483 | done: |
| 1484 | |
| 1485 | msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY; |
| 1486 | rmp = &msg.worker_add_del_reply; |
| 1487 | rmp->retval = rv; |
| 1488 | rmp->is_add = mp->is_add; |
| 1489 | rmp->wrk_index = args.wrk_map_index; |
| 1490 | rmp->segment_handle = args.segment_handle; |
| 1491 | if (!rv && mp->is_add) |
| 1492 | { |
| 1493 | /* No segment name and size. This supports only memfds */ |
| 1494 | rmp->app_event_queue_address = pointer_to_uword (args.evt_q); |
| 1495 | rmp->n_fds = n_fds; |
| 1496 | rmp->fd_flags = fd_flags; |
| 1497 | |
| 1498 | /* Update app index for socket */ |
| 1499 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1500 | app_wrk = application_get_worker (app, args.wrk_map_index); |
| 1501 | handle->aah_app_wrk_index = app_wrk->wrk_index; |
| 1502 | } |
| 1503 | |
| 1504 | clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds); |
| 1505 | } |
| 1506 | |
| 1507 | static void |
| 1508 | sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs) |
| 1509 | { |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1510 | app_ns_api_handle_t *handle; |
| 1511 | app_worker_t *app_wrk; |
| 1512 | u32 api_client_handle; |
| 1513 | |
| 1514 | api_client_handle = appns_sapi_socket_handle (app_ns, cs); |
| 1515 | sapi_socket_close_w_handle (api_client_handle); |
| 1516 | |
Florin Coras | f99a7d6 | 2020-09-14 10:29:29 -0700 | [diff] [blame^] | 1517 | /* Cleanup everything because app worker closed socket or crashed */ |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1518 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1519 | app_wrk = app_worker_get (handle->aah_app_wrk_index); |
Florin Coras | f99a7d6 | 2020-09-14 10:29:29 -0700 | [diff] [blame^] | 1520 | |
| 1521 | vnet_app_worker_add_del_args_t args = { |
| 1522 | .app_index = app_wrk->app_index, |
| 1523 | .wrk_map_index = app_wrk->wrk_map_index, |
| 1524 | .api_client_index = api_client_handle, |
| 1525 | .is_add = 0 |
| 1526 | }; |
| 1527 | /* Send rpc to main thread for worker barrier */ |
| 1528 | vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args, |
| 1529 | sizeof (args)); |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | static clib_error_t * |
| 1533 | sapi_sock_read_ready (clib_file_t * cf) |
| 1534 | { |
| 1535 | app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data; |
| 1536 | app_sapi_msg_t msg = { 0 }; |
| 1537 | app_namespace_t *app_ns; |
| 1538 | clib_error_t *err = 0; |
| 1539 | clib_socket_t *cs; |
| 1540 | |
| 1541 | app_ns = app_namespace_get (handle->aah_app_ns_index); |
| 1542 | cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index); |
| 1543 | if (!cs) |
| 1544 | goto error; |
| 1545 | |
| 1546 | err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0); |
| 1547 | if (err) |
| 1548 | { |
| 1549 | clib_error_free (err); |
| 1550 | sapi_socket_detach (app_ns, cs); |
| 1551 | goto error; |
| 1552 | } |
| 1553 | |
| 1554 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1555 | |
| 1556 | switch (msg.type) |
| 1557 | { |
| 1558 | case APP_SAPI_MSG_TYPE_ATTACH: |
| 1559 | session_api_attach_handler (app_ns, cs, &msg.attach); |
| 1560 | break; |
| 1561 | case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER: |
| 1562 | sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del); |
| 1563 | break; |
| 1564 | default: |
| 1565 | clib_warning ("app wrk %u unknown message type: %u", |
| 1566 | handle->aah_app_wrk_index, msg.type); |
| 1567 | break; |
| 1568 | } |
| 1569 | |
| 1570 | error: |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | static clib_error_t * |
| 1575 | sapi_sock_write_ready (clib_file_t * cf) |
| 1576 | { |
| 1577 | app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data; |
| 1578 | clib_warning ("called for app ns %u", handle->aah_app_ns_index); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | static clib_error_t * |
| 1583 | sapi_sock_error (clib_file_t * cf) |
| 1584 | { |
| 1585 | app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data; |
| 1586 | app_namespace_t *app_ns; |
| 1587 | clib_socket_t *cs; |
| 1588 | |
| 1589 | app_ns = app_namespace_get (handle->aah_app_ns_index); |
| 1590 | cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index); |
| 1591 | if (!cs) |
| 1592 | return 0; |
| 1593 | |
| 1594 | sapi_socket_detach (app_ns, cs); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | static clib_error_t * |
| 1599 | sapi_sock_accept_ready (clib_file_t * scf) |
| 1600 | { |
| 1601 | app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data; |
| 1602 | app_namespace_t *app_ns; |
| 1603 | clib_file_t cf = { 0 }; |
| 1604 | clib_error_t *err = 0; |
| 1605 | clib_socket_t *ccs, *scs; |
| 1606 | |
| 1607 | /* Listener files point to namespace */ |
| 1608 | app_ns = app_namespace_get (handle.aah_app_ns_index); |
| 1609 | |
| 1610 | /* |
| 1611 | * Initialize client socket |
| 1612 | */ |
| 1613 | ccs = appns_sapi_alloc_socket (app_ns); |
| 1614 | |
| 1615 | /* Grab server socket after client is initialized */ |
| 1616 | scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index); |
| 1617 | if (!scs) |
| 1618 | goto error; |
| 1619 | |
| 1620 | err = clib_socket_accept (scs, ccs); |
| 1621 | if (err) |
| 1622 | { |
| 1623 | clib_error_report (err); |
| 1624 | goto error; |
| 1625 | } |
| 1626 | |
| 1627 | cf.read_function = sapi_sock_read_ready; |
| 1628 | cf.write_function = sapi_sock_write_ready; |
| 1629 | cf.error_function = sapi_sock_error; |
| 1630 | cf.file_descriptor = ccs->fd; |
| 1631 | /* File points to app namespace and socket */ |
| 1632 | handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs); |
| 1633 | cf.private_data = handle.as_uword; |
| 1634 | cf.description = format (0, "app sock conn fd: %d", ccs->fd); |
| 1635 | |
| 1636 | /* Poll until we get an attach message. Socket points to file and |
| 1637 | * application that owns the socket */ |
| 1638 | handle.aah_app_wrk_index = APP_INVALID_INDEX; |
| 1639 | handle.aah_file_index = clib_file_add (&file_main, &cf); |
| 1640 | ccs->private_data = handle.as_uword; |
| 1641 | |
| 1642 | return err; |
| 1643 | |
| 1644 | error: |
| 1645 | appns_sapi_free_socket (app_ns, ccs); |
| 1646 | return err; |
| 1647 | } |
| 1648 | |
| 1649 | int |
| 1650 | appns_sapi_add_ns_socket (app_namespace_t * app_ns) |
| 1651 | { |
| 1652 | char *subdir = "/app_ns_sockets/"; |
| 1653 | app_ns_api_handle_t *handle; |
| 1654 | clib_file_t cf = { 0 }; |
| 1655 | struct stat file_stat; |
| 1656 | clib_error_t *err; |
| 1657 | clib_socket_t *cs; |
| 1658 | u8 *dir = 0; |
| 1659 | int rv = 0; |
| 1660 | |
| 1661 | vec_add (dir, vlib_unix_get_runtime_dir (), |
| 1662 | strlen (vlib_unix_get_runtime_dir ())); |
| 1663 | vec_add (dir, (u8 *) subdir, strlen (subdir)); |
| 1664 | |
| 1665 | err = vlib_unix_recursive_mkdir ((char *) dir); |
| 1666 | if (err) |
| 1667 | { |
| 1668 | clib_error_report (err); |
| 1669 | rv = -1; |
| 1670 | goto error; |
| 1671 | } |
| 1672 | |
| 1673 | app_ns->sock_name = format (0, "%v%v%c", dir, app_ns->ns_id, 0); |
| 1674 | |
| 1675 | /* |
| 1676 | * Create and initialize socket to listen on |
| 1677 | */ |
| 1678 | cs = appns_sapi_alloc_socket (app_ns); |
| 1679 | cs->config = (char *) app_ns->sock_name; |
| 1680 | cs->flags = CLIB_SOCKET_F_IS_SERVER | |
| 1681 | CLIB_SOCKET_F_ALLOW_GROUP_WRITE | |
| 1682 | CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED; |
| 1683 | |
| 1684 | if ((err = clib_socket_init (cs))) |
| 1685 | { |
| 1686 | clib_error_report (err); |
| 1687 | rv = -1; |
| 1688 | goto error; |
| 1689 | } |
| 1690 | |
| 1691 | if (stat ((char *) app_ns->sock_name, &file_stat) == -1) |
| 1692 | { |
| 1693 | rv = -1; |
| 1694 | goto error; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Start polling it |
| 1699 | */ |
| 1700 | cf.read_function = sapi_sock_accept_ready; |
| 1701 | cf.file_descriptor = cs->fd; |
| 1702 | /* File points to namespace */ |
| 1703 | handle = (app_ns_api_handle_t *) & cf.private_data; |
| 1704 | handle->aah_app_ns_index = app_namespace_index (app_ns); |
| 1705 | handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs); |
| 1706 | cf.description = format (0, "app sock listener: %s", app_ns->sock_name); |
| 1707 | |
| 1708 | /* Socket points to clib file index */ |
| 1709 | handle = (app_ns_api_handle_t *) & cs->private_data; |
| 1710 | handle->aah_file_index = clib_file_add (&file_main, &cf); |
| 1711 | handle->aah_app_wrk_index = APP_INVALID_INDEX; |
| 1712 | |
| 1713 | error: |
| 1714 | vec_free (dir); |
| 1715 | return rv; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1719 | * fd.io coding-style-patch-verification: ON |
| 1720 | * |
| 1721 | * Local Variables: |
| 1722 | * eval: (c-set-style "gnu") |
| 1723 | * End: |
| 1724 | */ |