litespeed-quic/src/liblsquic/lsquic_conn.c

340 lines
7.2 KiB
C
Raw Normal View History

/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc. See LICENSE. */
2017-09-22 21:00:03 +00:00
#include <assert.h>
#include <inttypes.h>
#include <string.h>
#include <sys/queue.h>
#include <openssl/rand.h>
2017-09-22 21:00:03 +00:00
#include "lsquic.h"
#include "lsquic_int_types.h"
#include "lsquic_hash.h"
2017-09-22 21:00:03 +00:00
#include "lsquic_conn.h"
#include "lsquic_packet_common.h"
#include "lsquic_packet_gquic.h"
2017-09-22 21:00:03 +00:00
#include "lsquic_packet_in.h"
#include "lsquic_str.h"
#include "lsquic_enc_sess.h"
2017-09-22 21:00:03 +00:00
#include "lsquic_mm.h"
#include "lsquic_engine_public.h"
#include "lsquic_ev_log.h"
#include "lsquic_logger.h"
const lsquic_cid_t *
2017-09-22 21:00:03 +00:00
lsquic_conn_id (const lsquic_conn_t *lconn)
{
/* TODO */
return lsquic_conn_log_cid(lconn);
2017-09-22 21:00:03 +00:00
}
void *
lsquic_conn_get_peer_ctx (struct lsquic_conn *lconn,
const struct sockaddr *local_sa)
2017-09-22 21:00:03 +00:00
{
const struct network_path *path;
path = lconn->cn_if->ci_get_path(lconn, local_sa);
return path->np_peer_ctx;
2017-09-22 21:00:03 +00:00
}
unsigned char
lsquic_conn_record_sockaddr (lsquic_conn_t *lconn, void *peer_ctx,
const struct sockaddr *local_sa, const struct sockaddr *peer_sa)
2017-09-22 21:00:03 +00:00
{
return lconn->cn_if->ci_record_addrs(lconn, peer_ctx, local_sa, peer_sa);
2017-09-22 21:00:03 +00:00
}
int
lsquic_conn_get_sockaddr (struct lsquic_conn *lconn,
2017-09-22 21:00:03 +00:00
const struct sockaddr **local, const struct sockaddr **peer)
{
const struct network_path *path;
path = lconn->cn_if->ci_get_path(lconn, NULL);
*local = NP_LOCAL_SA(path);
*peer = NP_PEER_SA(path);
return 0;
2017-09-22 21:00:03 +00:00
}
int
lsquic_conn_copy_and_release_pi_data (const lsquic_conn_t *conn,
struct lsquic_engine_public *enpub, lsquic_packet_in_t *packet_in)
{
unsigned char *copy;
2017-09-22 21:00:03 +00:00
assert(!(packet_in->pi_flags & PI_OWN_DATA));
copy = lsquic_mm_get_packet_in_buf(&enpub->enp_mm, packet_in->pi_data_sz);
2017-09-22 21:00:03 +00:00
if (!copy)
{
LSQ_WARN("cannot allocate memory to copy incoming packet data");
return -1;
}
memcpy(copy, packet_in->pi_data, packet_in->pi_data_sz);
packet_in->pi_data = copy;
packet_in->pi_flags |= PI_OWN_DATA;
return 0;
}
enum lsquic_version
lsquic_conn_quic_version (const lsquic_conn_t *lconn)
{
if (lconn->cn_flags & LSCONN_VER_SET)
return lconn->cn_version;
else
2017-09-22 21:00:03 +00:00
return -1;
}
2017-09-22 21:00:03 +00:00
Latest changes - [API Change] Sendfile-like functionality is gone. The stream no longer opens files and deals with file descriptors. (Among other things, this makes the code more portable.) Three writing functions are provided: lsquic_stream_write lsquic_stream_writev lsquic_stream_writef (NEW) lsquic_stream_writef() is given an abstract reader that has function pointers for size() and read() functions which the user can implement. This is the most flexible way. lsquic_stream_write() and lsquic_stream_writev() are now both implemented as wrappers around lsquic_stream_writef(). - [OPTIMIZATION] When writing to stream, be it within or without the on_write() callback, place data directly into packet buffer, bypassing auxiliary data structures. This reduces amount of memory required, for the amount of data that can be written is limited by the congestion window. To support writes outside the on_write() callback, we keep N outgoing packet buffers per connection which can be written to by any stream. One half of these are reserved for the highest priority stream(s), the other half for all other streams. This way, low-priority streams cannot write instead of high-priority streams and, on the other hand, low-priority streams get a chance to send their packets out. The algorithm is as follows: - When user writes to stream outside of the callback: - If this is the highest priority stream, place it onto the reserved N/2 queue or fail. (The actual size of this queue is dynamic -- MAX(N/2, CWND) -- rather than N/2, allowing high-priority streams to write as much as can be sent.) - If the stream is not the highest priority, try to place the data onto the reserved N/2 queue or fail. - When tick occurs *and* more packets can be scheduled: - Transfer packets from the high N/2 queue to the scheduled queue. - If more scheduling is allowed: - Call on_write callbacks for highest-priority streams, placing resulting packets directly onto the scheduled queue. - If more scheduling is allowed: - Transfer packets from the low N/2 queue to the scheduled queue. - If more scheduling is allowed: - Call on_write callbacks for non-highest-priority streams, placing resulting packets directly onto the scheduled queue The number N is currently 20, but it could be varied based on resource usage. - If stream is created due to incoming headers, make headers readable from on_new. - Outgoing packets are no longer marked non-writeable to prevent placing more than one STREAM frame from the same stream into a single packet. This property is maintained via code flow and an explicit check. Packets for stream data are allocated using a special function. - STREAM frame elision is cheaper, as we only perform it if a reset stream has outgoing packets referencing it. - lsquic_packet_out_t is smaller, as stream_rec elements are now inside a union.
2017-10-31 13:35:58 +00:00
enum lsquic_crypto_ver
lsquic_conn_crypto_ver (const lsquic_conn_t *lconn)
{
return LSQ_CRY_QUIC;
2017-09-22 21:00:03 +00:00
}
const char *
lsquic_conn_crypto_cipher (const lsquic_conn_t *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_cipher(lconn->cn_enc_session);
else
return NULL;
}
int
lsquic_conn_crypto_keysize (const lsquic_conn_t *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_keysize(lconn->cn_enc_session);
else
return -1;
}
int
lsquic_conn_crypto_alg_keysize (const lsquic_conn_t *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_alg_keysize(lconn->cn_enc_session);
else
return -1;
}
struct stack_st_X509 *
lsquic_conn_get_server_cert_chain (struct lsquic_conn *lconn)
{
if (lconn->cn_enc_session)
return lconn->cn_esf_c->esf_get_server_cert_chain(lconn->cn_enc_session);
else
return NULL;
}
void
lsquic_conn_make_stream (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_make_stream(lconn);
}
unsigned
lsquic_conn_n_pending_streams (const struct lsquic_conn *lconn)
{
return lconn->cn_if->ci_n_pending_streams(lconn);
}
unsigned
lsquic_conn_n_avail_streams (const struct lsquic_conn *lconn)
{
return lconn->cn_if->ci_n_avail_streams(lconn);
}
unsigned
lsquic_conn_cancel_pending_streams (struct lsquic_conn *lconn, unsigned count)
{
return lconn->cn_if->ci_cancel_pending_streams(lconn, count);
}
void
lsquic_conn_going_away (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_going_away(lconn);
}
void
lsquic_conn_close (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_close(lconn);
}
int
lsquic_conn_is_push_enabled (lsquic_conn_t *lconn)
{
return lconn->cn_if->ci_is_push_enabled(lconn);
}
struct lsquic_engine *
lsquic_conn_get_engine (struct lsquic_conn *lconn)
{
return lconn->cn_if->ci_get_engine(lconn);
}
int
lsquic_conn_push_stream (struct lsquic_conn *lconn, void *hset,
struct lsquic_stream *stream, const struct lsquic_http_headers *headers)
{
return lconn->cn_if->ci_push_stream(lconn, hset, stream, headers);
}
lsquic_conn_ctx_t *
lsquic_conn_get_ctx (const struct lsquic_conn *lconn)
{
return lconn->cn_conn_ctx;
}
void
lsquic_conn_set_ctx (struct lsquic_conn *lconn, lsquic_conn_ctx_t *ctx)
{
lconn->cn_conn_ctx = ctx;
}
void
lsquic_conn_abort (struct lsquic_conn *lconn)
{
lconn->cn_if->ci_abort(lconn);
}
void
lsquic_generate_cid (lsquic_cid_t *cid, size_t len)
{
if (!len)
{
/* If not set, generate ID between 8 and MAX_CID_LEN bytes in length */
RAND_bytes((uint8_t *) &len, sizeof(len));
len %= MAX_CID_LEN - 7;
len += 8;
}
RAND_bytes(cid->idbuf, len);
cid->len = len;
}
void
lsquic_generate_scid (void *ctx, struct lsquic_conn *lconn, lsquic_cid_t *scid,
unsigned len)
{
if (len)
lsquic_generate_cid(scid, len);
else
scid->len = len;
}
void
lsquic_generate_cid_gquic (lsquic_cid_t *cid)
{
lsquic_generate_cid(cid, GQUIC_CID_LEN);
}
void
lsquic_conn_retire_cid (struct lsquic_conn *lconn)
{
if (lconn->cn_if->ci_retire_cid)
lconn->cn_if->ci_retire_cid(lconn);
}
enum LSQUIC_CONN_STATUS
lsquic_conn_status (struct lsquic_conn *lconn, char *errbuf, size_t bufsz)
{
return lconn->cn_if->ci_status(lconn, errbuf, bufsz);
}
const lsquic_cid_t *
lsquic_conn_log_cid (const struct lsquic_conn *lconn)
{
if (lconn->cn_if && lconn->cn_if->ci_get_log_cid)
return lconn->cn_if->ci_get_log_cid(lconn);
return CN_SCID(lconn);
}
int
lsquic_conn_want_datagram_write (struct lsquic_conn *lconn, int is_want)
{
if (lconn->cn_if && lconn->cn_if->ci_want_datagram_write)
return lconn->cn_if->ci_want_datagram_write(lconn, is_want);
else
return -1;
}
int
lsquic_conn_set_min_datagram_size (struct lsquic_conn *lconn, size_t sz)
{
if (lconn->cn_if && lconn->cn_if->ci_set_min_datagram_size)
return lconn->cn_if->ci_set_min_datagram_size(lconn, sz);
else
return -1;
}
size_t
lsquic_conn_get_min_datagram_size (struct lsquic_conn *lconn)
{
if (lconn->cn_if && lconn->cn_if->ci_get_min_datagram_size)
return lconn->cn_if->ci_get_min_datagram_size(lconn);
else
return 0;
}
#if LSQUIC_CONN_STATS
void
lsquic_conn_stats_diff (const struct conn_stats *cumulative_stats,
const struct conn_stats *previous_stats,
struct conn_stats *new_stats)
{
const unsigned long *const cum = (void *) cumulative_stats,
*const prev = (void *) previous_stats;
unsigned long *const new = (void *) new_stats;
unsigned i;
for (i = 0; i < sizeof(*new_stats) / sizeof(new[0]); ++i)
new[i] = cum[i] - prev[i];
}
#endif
const char *
lsquic_conn_get_sni (struct lsquic_conn *lconn)
{
if (lconn->cn_esf_c && lconn->cn_esf_c->esf_get_sni)
return lconn->cn_esf_c->esf_get_sni(lconn->cn_enc_session);
else
return NULL;
}