tldk_sock.h revision e18a033b
1/*
2 * Copyright (c) 2017  Intel Corporation.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#ifndef __TLDK_SOCK_H__
28#define __TLDK_SOCK_H__
29
30#include <stdint.h>
31#include <stddef.h>
32#include <inttypes.h>
33
34#include <ngx_config.h>
35#include <ngx_core.h>
36#include <ngx_event.h>
37
38#include <tle_tcp.h>
39
40extern ngx_module_t ngx_tldk_event_module;
41
42extern int tldk_stbl_init(const ngx_cycle_t *, const struct tldk_ctx *);
43extern void tldk_stbl_fini(void);
44
45extern int
46tldk_open_bind_listen(struct tldk_ctx *tcx, int domain, int type,
47	const struct sockaddr *addr, socklen_t addrlen, int backlog);
48
49extern void
50tldk_dump_event_stats(void);
51
52#define	TLDK_ACCEPT_BULK	0x10
53
54struct tldk_sock {
55	LIST_ENTRY(tldk_sock) link;
56	struct tle_stream *s;
57	struct tle_event *erev;
58	struct tle_event *rxev;
59	struct tle_event *txev;
60	ngx_event_t *rev;
61	ngx_event_t *wev;
62	uint16_t posterr;
63	struct {
64		uint32_t num;
65		struct tle_stream *buf[TLDK_ACCEPT_BULK];
66	} acpt;
67};
68
69struct tldk_sock_list {
70        uint32_t num;
71        LIST_HEAD(, tldk_sock) head;
72};
73
74struct tldk_stbl {
75        struct tle_evq *syneq;
76        struct tle_evq *ereq;
77        struct tle_evq *rxeq;
78        struct tle_evq *txeq;
79        struct tldk_sock_list free;
80        struct tldk_sock_list lstn;
81        struct tldk_sock_list use;
82        int32_t nosd;
83        uint32_t snum;
84        struct tldk_sock *sd;
85};
86
87
88#define SOCK_TO_SD(s)   (stbl.nosd + ((s) - stbl.sd))
89#define SD_TO SOCK(d)   (stbl.sd + ((d) - stbl.nosd))
90
91/* One socket/file table per worker */
92extern struct tldk_stbl stbl;
93
94static inline struct tldk_sock *
95sd_to_sock(int32_t sd)
96{
97        uint32_t n;
98
99        n = sd - stbl.nosd;
100        if (n >= stbl.snum)
101                return NULL;
102
103        return stbl.sd + n;
104}
105
106
107
108#endif /* __TLDK_SOCK_H__ */
109