aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtle_glue/sock.h
blob: fcd6362bfa6175530fdcaac0883c18bf59eac9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2018 Ant Financial Services Group.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _SOCK_H_
#define _SOCK_H_

#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <tle_event.h>
#include <tle_ctx.h>

#include "ctx.h"

#ifdef __cplusplus
extern "C" {
#endif

extern unsigned int def_sndbuf;
extern unsigned int def_rcvbuf;

#ifndef TCP_FASTOPEN
#define TCP_FASTOPEN 23
#endif

#ifndef TCP_USER_TIMEOUT
#define TCP_USER_TIMEOUT 18
#endif

#ifndef TCP_FASTOPEN_CONNECT
#define TCP_FASTOPEN_CONNECT	30
#endif

struct sock;

struct proto {
	int (*setsockopt)(struct sock *sk, int optname, const void *optval,
			  socklen_t optlen);
	int (*getsockopt)(struct sock *sk, int optname, void *optval,
			  socklen_t *option);
	int (*getname)(struct sock *sk, struct sockaddr *addr, int peer);

	int (*bind)(struct sock *sk, const struct sockaddr *addr);
	int (*listen)(struct sock *sk, int backlog);
	int (*connect)(struct sock *sk, const struct sockaddr *addr);
	int (*accept)(struct sock *sk, struct sockaddr *addr,
		      socklen_t *addrlen, int flags);

	ssize_t (*recv)(struct tle_stream *s, struct rte_mbuf *pkt[],
			uint16_t num, struct sockaddr *addr);
	ssize_t (*send)(struct sock *sk, struct rte_mbuf *pkt[],
			uint16_t num, const struct sockaddr *dst_addr);

	ssize_t (*readv)(struct tle_stream *s, struct msghdr *msg, int flags);
	ssize_t (*writev)(struct sock *sk, const struct iovec *iov,
			  int iovcnt, const struct sockaddr *dst_addr);

	int (*shutdown)(struct sock *sk, int how);
	int (*close)(struct tle_stream *s);

	void (*update_cfg)(struct sock *sk);

	char name[32];
};

enum {
	PROTO_TCP,
	PROTO_UDP
};

#define RECV_SHUTDOWN	1
#define SEND_SHUTDOWN	2

extern struct proto udp_prot;
extern struct proto tcp_prot;
extern struct proto *supported_proto_ops[];

struct sock {
	int		    fd;
	uint32_t	    cid:8,     /* ctx id for indexing ctx_array */
			    domain:8,  /* for AF_INET, AF_INET6 */
			    proto:8,   /* PROTO_TCP, PROTO_UDP */
			    valid:1,
			    epoll:1,
			    ubind:1,
			    ubindany:1,
			    nonblock:1,
			    tcp_connected:1,
			    shutdown:2;
	struct tle_stream   *s;
	struct rte_mbuf     *rx_left;
	tle_stream_options_t option;
	union {
		struct epoll_event event;
		int	    shadow_efd;
	};
	struct tle_event    txev;
	struct tle_event    rxev;
	struct tle_event    erev;
} __rte_cache_aligned;

#define CTX(so)    (&ctx_array[so->cid])
#define OPS(so)    (supported_proto_ops[so->proto])
#define IS_TCP(so) (so->proto == PROTO_TCP)
#define IS_UDP(so) (so->proto == PROTO_UDP)

static inline int
is_nonblock(struct sock *so, int flags)
{
	return (flags & MSG_DONTWAIT) || so->nonblock;
}

static inline struct tle_ctx *
get_sock_ctx(struct sock *so)
{
	if (IS_TCP(so))
		return CTX(so)->tcp_ctx;
	else
		return CTX(so)->udp_ctx;
}

static inline size_t
get_sockaddr_len(sa_family_t family)
{
	switch (family) {
	case AF_INET:
		return sizeof(struct sockaddr_in);
	case AF_INET6:
		return sizeof(struct sockaddr_in6);
	case AF_UNSPEC:
		return sizeof(sa_family_t);
	default:
		return 0;
	}
}

#ifdef __cplusplus
}
#endif

#endif /*_SOCK_H_ */