|
| 1 | +/** @file |
| 2 | + * |
| 3 | + * OpenSSL socket BIO that does TCP Fast Open. |
| 4 | + * |
| 5 | + * @section license License |
| 6 | + * |
| 7 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 8 | + * or more contributor license agreements. See the NOTICE file |
| 9 | + * distributed with this work for additional information |
| 10 | + * regarding copyright ownership. The ASF licenses this file |
| 11 | + * to you under the Apache License, Version 2.0 (the |
| 12 | + * "License"); you may not use this file except in compliance |
| 13 | + * with the License. You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + */ |
| 23 | + |
| 24 | +#include "I_Net.h" |
| 25 | +#include "I_SocketManager.h" |
| 26 | +#include "ts/ink_assert.h" |
| 27 | + |
| 28 | +#include "BIO_fastopen.h" |
| 29 | + |
| 30 | +static int |
| 31 | +fastopen_create(BIO *bio) |
| 32 | +{ |
| 33 | + bio->init = 0; |
| 34 | + bio->num = NO_FD; |
| 35 | + bio->flags = 0; |
| 36 | + bio->ptr = NULL; |
| 37 | + |
| 38 | + return 1; |
| 39 | +} |
| 40 | + |
| 41 | +static int |
| 42 | +fastopen_destroy(BIO *bio) |
| 43 | +{ |
| 44 | + if (bio) { |
| 45 | + // We expect this BIO to not own the socket, so we must always |
| 46 | + // be in NOCLOSE mode. |
| 47 | + ink_assert(bio->shutdown == BIO_NOCLOSE); |
| 48 | + fastopen_create(bio); |
| 49 | + } |
| 50 | + |
| 51 | + return 1; |
| 52 | +} |
| 53 | + |
| 54 | +static int |
| 55 | +fastopen_bwrite(BIO *bio, const char *in, int insz) |
| 56 | +{ |
| 57 | + int64_t err; |
| 58 | + |
| 59 | + errno = 0; |
| 60 | + BIO_clear_retry_flags(bio); |
| 61 | + ink_assert(bio->num != NO_FD); |
| 62 | + |
| 63 | + if (bio->ptr) { |
| 64 | + // On the first write only, make a TFO request if TFO is enabled. |
| 65 | + // The best documentation on the behavior of the Linux API is in |
| 66 | + // RFC 7413. If we get EINPROGRESS it means that the SYN has been |
| 67 | + // sent without data dn we sshould retry. |
| 68 | + const sockaddr *dst = reinterpret_cast<const sockaddr *>(bio->ptr); |
| 69 | + |
| 70 | + err = socketManager.sendto(bio->num, (void *)in, insz, MSG_FASTOPEN, dst, ats_ip_size(dst)); |
| 71 | + |
| 72 | + bio->ptr = NULL; |
| 73 | + } else { |
| 74 | + err = socketManager.write(bio->num, (void *)in, insz); |
| 75 | + } |
| 76 | + |
| 77 | + if (err < 0) { |
| 78 | + errno = -err; |
| 79 | + if (BIO_sock_non_fatal_error(errno)) { |
| 80 | + BIO_set_retry_write(bio); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return err < 0 ? -1 : err; |
| 85 | +} |
| 86 | + |
| 87 | +static int |
| 88 | +fastopen_bread(BIO *bio, char *out, int outsz) |
| 89 | +{ |
| 90 | + int64_t err; |
| 91 | + |
| 92 | + errno = 0; |
| 93 | + BIO_clear_retry_flags(bio); |
| 94 | + ink_assert(bio->num != NO_FD); |
| 95 | + |
| 96 | + // TODO: If we haven't done the fastopen, ink_abort(). |
| 97 | + |
| 98 | + err = socketManager.read(bio->num, out, outsz); |
| 99 | + if (err < 0) { |
| 100 | + errno = -err; |
| 101 | + if (BIO_sock_non_fatal_error(errno)) { |
| 102 | + BIO_set_retry_write(bio); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return err < 0 ? -1 : err; |
| 107 | +} |
| 108 | + |
| 109 | +static long |
| 110 | +fastopen_ctrl(BIO *bio, int cmd, long larg, void *ptr) |
| 111 | +{ |
| 112 | + switch (cmd) { |
| 113 | + case BIO_C_SET_FD: |
| 114 | + ink_assert(larg == BIO_CLOSE || larg == BIO_NOCLOSE); |
| 115 | + ink_assert(bio->num == NO_FD); |
| 116 | + |
| 117 | + bio->init = 1; |
| 118 | + bio->shutdown = larg; |
| 119 | + bio->num = *reinterpret_cast<int *>(ptr); |
| 120 | + return 0; |
| 121 | + |
| 122 | + case BIO_C_SET_CONNECT: |
| 123 | + // We only support BIO_set_conn_address(), which sets a sockaddr. |
| 124 | + ink_assert(larg == 2); |
| 125 | + bio->ptr = ptr; |
| 126 | + return 0; |
| 127 | + |
| 128 | + // We are unbuffered so unconditionally succeed on BIO_flush(). |
| 129 | + case BIO_CTRL_FLUSH: |
| 130 | + return 1; |
| 131 | + |
| 132 | + case BIO_CTRL_PUSH: |
| 133 | + case BIO_CTRL_POP: |
| 134 | + return 0; |
| 135 | + |
| 136 | + default: |
| 137 | +#if DEBUG |
| 138 | + ink_abort("unsupported BIO control cmd=%d larg=%ld ptr=%p", cmd, larg, ptr); |
| 139 | +#endif |
| 140 | + |
| 141 | + return 0; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +static const BIO_METHOD fastopen_methods = { |
| 146 | + .type = BIO_TYPE_SOCKET, |
| 147 | + .name = "fastopen", |
| 148 | + .bwrite = fastopen_bwrite, |
| 149 | + .bread = fastopen_bread, |
| 150 | + .bputs = NULL, |
| 151 | + .bgets = NULL, |
| 152 | + .ctrl = fastopen_ctrl, |
| 153 | + .create = fastopen_create, |
| 154 | + .destroy = fastopen_destroy, |
| 155 | + .callback_ctrl = NULL, |
| 156 | +}; |
| 157 | + |
| 158 | +const BIO_METHOD * |
| 159 | +BIO_s_fastopen() |
| 160 | +{ |
| 161 | + return &fastopen_methods; |
| 162 | +} |
0 commit comments