FD.io VPP  v21.06
Vector Packet Processing
tls_bio.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <openssl/bio.h>
17 #include <openssl/err.h>
18 #include <vnet/session/session.h>
20 
21 static inline session_t *
22 bio_session (BIO * bio)
23 {
24  return session_get_from_handle (pointer_to_uword (BIO_get_data (bio)));
25 }
26 
27 static int
28 bio_tls_alloc (BIO * bio)
29 {
30  BIO_set_init (bio, 0);
31  BIO_set_data (bio, 0);
32  BIO_set_flags (bio, 0);
33  BIO_set_shutdown (bio, 0);
34  return 1;
35 }
36 
37 static int
38 bio_tls_free (BIO * bio)
39 {
40  if (!bio)
41  return 0;
42 
43  if (BIO_get_shutdown (bio))
44  {
45  if (BIO_get_init (bio))
46  session_close (bio_session (bio));
47  BIO_set_init (bio, 0);
48  BIO_set_flags (bio, 0);
49  }
50 
51  return 1;
52 }
53 
54 static int
55 bio_tls_read (BIO * b, char *out, int outl)
56 {
57  session_t *s;
58  int rv;
59 
60  if (PREDICT_FALSE (!out))
61  return 0;
62 
63  s = bio_session (b);
64  if (!s)
65  {
66  clib_warning ("no session");
67  errno = EBADFD;
68  return -1;
69  }
70 
71  rv = app_recv_stream_raw (s->rx_fifo, (u8 *) out, outl,
72  0 /* clear evt */ , 0 /* peek */ );
73  if (rv < 0)
74  {
75  BIO_set_retry_read (b);
76  errno = EAGAIN;
77  return -1;
78  }
79 
80  if (svm_fifo_needs_deq_ntf (s->rx_fifo, rv))
81  {
84  }
85 
88 
89  BIO_clear_retry_flags (b);
90 
91  return rv;
92 }
93 
94 static int
95 bio_tls_write (BIO * b, const char *in, int inl)
96 {
97  svm_msg_q_t *mq;
98  session_t *s;
99  int rv;
100 
101  if (PREDICT_FALSE (!in))
102  return 0;
103 
104  s = bio_session (b);
105  if (!s)
106  {
107  clib_warning ("no session");
108  errno = EBADFD;
109  return -1;
110  }
111 
113  rv = app_send_stream_raw (s->tx_fifo, mq, (u8 *) in, inl,
114  SESSION_IO_EVT_TX, 1 /* do_evt */ ,
115  0 /* noblock */ );
116  if (rv < 0)
117  {
118  BIO_set_retry_write (b);
119  errno = EAGAIN;
120  return -1;
121  }
122 
123  BIO_clear_retry_flags (b);
124 
125  return rv;
126 }
127 
128 long
129 bio_tls_ctrl (BIO * b, int cmd, long larg, void *ptr)
130 {
131  long ret = 1;
132 
133  switch (cmd)
134  {
135  case BIO_C_SET_FD:
136  ASSERT (0);
137  break;
138  case BIO_C_GET_FD:
139  ASSERT (0);
140  break;
141  case BIO_CTRL_GET_CLOSE:
142  ret = BIO_get_shutdown (b);
143  break;
144  case BIO_CTRL_SET_CLOSE:
145  BIO_set_shutdown (b, (int) larg);
146  break;
147  case BIO_CTRL_DUP:
148  case BIO_CTRL_FLUSH:
149  ret = 1;
150  break;
151  case BIO_CTRL_PENDING:
152  ret = 0;
153  break;
154  default:
155  ret = 0;
156  break;
157  }
158  return ret;
159 }
160 
161 BIO *
163 {
164  static BIO_METHOD *tls_bio_method;
165  BIO *b;
166  if (!tls_bio_method)
167  {
168  tls_bio_method = BIO_meth_new (BIO_TYPE_SOCKET, "tls_bio");
169  BIO_meth_set_write (tls_bio_method, bio_tls_write);
170  BIO_meth_set_read (tls_bio_method, bio_tls_read);
171  BIO_meth_set_create (tls_bio_method, bio_tls_alloc);
172  BIO_meth_set_destroy (tls_bio_method, bio_tls_free);
173  BIO_meth_set_ctrl (tls_bio_method, bio_tls_ctrl);
174  }
175  b = BIO_new (tls_bio_method);
176  /* Initialize the BIO */
177  BIO_set_data (b, uword_to_pointer (sh, void *));
178  BIO_set_init (b, 1);
179  return b;
180 }
181 
182 /*
183  * fd.io coding-style-patch-verification: ON
184  *
185  * Local Variables:
186  * eval: (c-set-style "gnu")
187  * End:
188  */
static int app_recv_stream_raw(svm_fifo_t *f, u8 *buf, u32 len, u8 clear_evt, u8 peek)
svm_fifo_t * tx_fifo
static int bio_tls_alloc(BIO *bio)
Definition: tls_bio.c:28
static svm_msg_q_t * session_main_get_vpp_event_queue(u32 thread_index)
Definition: session.h:716
svm_fifo_t * rx_fifo
Pointers to rx/tx buffers.
static int svm_fifo_is_empty_cons(svm_fifo_t *f)
Check if fifo is empty optimized for consumer.
Definition: svm_fifo.h:554
unsigned char u8
Definition: types.h:56
vlib_buffer_t ** b
BIO * BIO_new_tls(session_handle_t sh)
Definition: tls_bio.c:162
static session_t * bio_session(BIO *bio)
Definition: tls_bio.c:22
int __clib_unused rv
Definition: application.c:491
int session_send_io_evt_to_thread(svm_fifo_t *f, session_evt_type_t evt_type)
Definition: session.c:84
static session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:356
#define PREDICT_FALSE(x)
Definition: clib.h:124
static void svm_fifo_unset_event(svm_fifo_t *f)
Unset fifo event flag.
Definition: svm_fifo.h:803
static u8 svm_fifo_needs_deq_ntf(svm_fifo_t *f, u32 n_last_deq)
Check if fifo needs dequeue notification.
Definition: svm_fifo.h:882
#define clib_warning(format, args...)
Definition: error.h:59
static int bio_tls_read(BIO *b, char *out, int outl)
Definition: tls_bio.c:55
static int bio_tls_write(BIO *b, const char *in, int inl)
Definition: tls_bio.c:95
#define uword_to_pointer(u, type)
Definition: types.h:136
#define ASSERT(truth)
static void svm_fifo_clear_deq_ntf(svm_fifo_t *f)
Clear the want notification flag and set has notification.
Definition: svm_fifo.h:848
static uword pointer_to_uword(const void *p)
Definition: types.h:131
u8 thread_index
Index of the thread that allocated the session.
static int bio_tls_free(BIO *bio)
Definition: tls_bio.c:38
u64 session_handle_t
void session_close(session_t *s)
Initialize session closing procedure.
Definition: session.c:1496
static int app_send_stream_raw(svm_fifo_t *f, svm_msg_q_t *vpp_evt_q, u8 *data, u32 len, u8 evt_type, u8 do_evt, u8 noblock)
long bio_tls_ctrl(BIO *b, int cmd, long larg, void *ptr)
Definition: tls_bio.c:129