PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/bsd/sys/sys/socketvar.h

https://gitlab.com/jforge/osv
C Header | 399 lines | 248 code | 39 blank | 112 comment | 16 complexity | 4362d8217cf98d06ea5e334fd5ea28e2 MD5 | raw file
Possible License(s): BSD-3-Clause, 0BSD, MPL-2.0-no-copyleft-exception
  1. /*-
  2. * Copyright (c) 1982, 1986, 1990, 1993
  3. * The Regents of the University of California. 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. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
  30. *
  31. * $FreeBSD$
  32. */
  33. #ifndef _SYS_SOCKETVAR_H_
  34. #define _SYS_SOCKETVAR_H_
  35. #include <bsd/porting/netport.h>
  36. #include <bsd/sys/sys/queue.h> /* for TAILQ macros */
  37. #include <bsd/sys/sys/sockbuf.h>
  38. #include <bsd/sys/sys/sockstate.h>
  39. #ifdef _KERNEL
  40. #include <bsd/sys/sys/sockopt.h>
  41. #endif
  42. #include <osv/net_channel.hh>
  43. struct vnet;
  44. /*
  45. * Kernel structure per socket.
  46. * Contains send and receive buffer queues,
  47. * handle on protocol and pointer to protocol
  48. * private data and error information.
  49. */
  50. typedef u_quad_t so_gen_t;
  51. struct socket;
  52. struct file;
  53. /*-
  54. * Locking key to struct socket:
  55. * (a) constant after allocation, no locking required.
  56. * (b) locked by SOCK_LOCK(so).
  57. * (c) locked by SOCK_LOCK(so).
  58. * (d) locked by SOCK_LOCK(so).
  59. * (e) locked by ACCEPT_LOCK().
  60. * (f) not locked since integer reads/writes are atomic.
  61. * (g) used only as a sleep/wakeup address, no value.
  62. * (h) locked by global mutex so_global_mtx.
  63. */
  64. struct socket {
  65. mutex* so_mtx = nullptr; /* provided by so_pcb */
  66. int so_count; /* (b) reference count */
  67. short so_type; /* (a) generic type, see socket.h */
  68. short so_options; /* from socket call, see socket.h */
  69. short so_linger; /* time to linger while closing */
  70. short so_state; /* (b) internal state flags SS_* */
  71. int so_qstate; /* (e) internal state flags SQ_* */
  72. void *so_pcb; /* protocol control block */
  73. struct vnet *so_vnet; /* network stack instance */
  74. struct protosw *so_proto; /* (a) protocol handle */
  75. /*
  76. * Variables for connection queuing.
  77. * Socket where accepts occur is so_head in all subsidiary sockets.
  78. * If so_head is 0, socket is not related to an accept.
  79. * For head socket so_incomp queues partially completed connections,
  80. * while so_comp is a queue of connections ready to be accepted.
  81. * If a connection is aborted and it has so_head set, then
  82. * it has to be pulled out of either so_incomp or so_comp.
  83. * We allow connections to queue up based on current queue lengths
  84. * and limit on number of queued connections for this socket.
  85. */
  86. struct socket *so_head; /* (e) back pointer to listen socket */
  87. TAILQ_HEAD(, socket) so_incomp; /* (e) queue of partial unaccepted connections */
  88. TAILQ_HEAD(, socket) so_comp; /* (e) queue of complete unaccepted connections */
  89. TAILQ_ENTRY(socket) so_list; /* (e) list of unaccepted connections */
  90. u_short so_qlen; /* (e) number of unaccepted connections */
  91. u_short so_incqlen; /* (e) number of unaccepted incomplete
  92. connections */
  93. u_short so_qlimit; /* (e) max number queued connections */
  94. short so_timeo; /* (g) connection timeout */
  95. u_short so_error; /* (f) error affecting connection */
  96. u_long so_oobmark; /* (c) chars to oob mark */
  97. TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
  98. struct sockbuf so_rcv, so_snd;
  99. /* NB: generation count must not be first. */
  100. int so_gencnt; /* (h) generation count */
  101. void *so_emuldata; /* (b) private data for emulators */
  102. struct so_accf {
  103. struct accept_filter *so_accept_filter;
  104. void *so_accept_filter_arg; /* saved filter args */
  105. char *so_accept_filter_str; /* saved user args */
  106. } *so_accf;
  107. /*
  108. * so_fibnum, so_user_cookie and friends can be used to attach
  109. * some user-specified metadata to a socket, which then can be
  110. * used by the kernel for various actions.
  111. * so_user_cookie is used by ipfw/dummynet.
  112. */
  113. int so_fibnum; /* routing domain for this socket */
  114. uint32_t so_user_cookie;
  115. net_channel* so_nc = nullptr;
  116. // a net channel only supports one consumer, so let others wait on a waitqueue instead
  117. bool so_nc_busy = false;
  118. waitqueue so_nc_wq;
  119. /* FIXME: this is done for poll,
  120. * make sure there's only 1 ref to a fp */
  121. struct file* fp;
  122. void set_mutex(mutex* a_mtx) { so_mtx = a_mtx; }
  123. };
  124. /*
  125. * Global accept mutex to serialize access to accept queues and
  126. * fields associated with multiple sockets. This allows us to
  127. * avoid defining a lock order between listen and accept sockets
  128. * until such time as it proves to be a good idea.
  129. */
  130. extern struct mtx accept_mtx;
  131. #define ACCEPT_LOCK_ASSERT() mtx_assert(&accept_mtx, MA_OWNED)
  132. #define ACCEPT_UNLOCK_ASSERT() mtx_assert(&accept_mtx, MA_NOTOWNED)
  133. #define ACCEPT_LOCK() mtx_lock(&accept_mtx)
  134. #define ACCEPT_UNLOCK() mtx_unlock(&accept_mtx)
  135. #define SOCK_MTX(_so) ((_so)->so_mtx)
  136. #define SOCK_MTX_REF(_so) (*SOCK_MTX(so))
  137. #define SOCK_LOCK(_so) (SOCK_MTX(_so)->lock())
  138. #define SOCK_OWNED(_so) (SOCK_MTX(_so)->owned())
  139. #define SOCK_UNLOCK(_so) (SOCK_MTX(_so)->unlock())
  140. #define SOCK_LOCK_ASSERT(_so) assert(SOCK_OWNED(_so))
  141. #define SOCK_UNLOCK_ASSERT(_so) assert(!SOCK_OWNED(_so))
  142. /*
  143. * Socket state bits stored in so_qstate.
  144. */
  145. #define SQ_INCOMP 0x0800 /* unaccepted, incomplete connection */
  146. #define SQ_COMP 0x1000 /* unaccepted, complete connection */
  147. /*
  148. * Externalized form of struct socket used by the sysctl(3) interface.
  149. */
  150. struct xsocket {
  151. size_t xso_len; /* length of this structure */
  152. struct socket *xso_so; /* makes a convenient handle sometimes */
  153. short so_type;
  154. short so_options;
  155. short so_linger;
  156. short so_state;
  157. caddr_t so_pcb; /* another convenient handle */
  158. int xso_protocol;
  159. int xso_family;
  160. u_short so_qlen;
  161. u_short so_incqlen;
  162. u_short so_qlimit;
  163. short so_timeo;
  164. u_short so_error;
  165. pid_t so_pgid;
  166. u_long so_oobmark;
  167. struct xsockbuf so_rcv, so_snd;
  168. uid_t so_uid; /* XXX */
  169. };
  170. #ifdef _KERNEL
  171. /*
  172. * Macros for sockets and socket buffering.
  173. */
  174. /*
  175. * Flags to sblock().
  176. */
  177. #define SBL_WAIT 0x00000001 /* Wait if not immediately available. */
  178. #define SBL_NOINTR 0x00000002 /* Force non-interruptible sleep. */
  179. #define SBL_VALID (SBL_WAIT | SBL_NOINTR)
  180. /*
  181. * Do we need to notify the other side when I/O is possible?
  182. */
  183. #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
  184. SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
  185. /* do we have to send all at once on a socket? */
  186. #define sosendallatonce(so) \
  187. ((so)->so_proto->pr_flags & PR_ATOMIC)
  188. /* can we read something from so? */
  189. #define soreadabledata(so) \
  190. ((so)->so_rcv.sb_cc >= (u_int)(so)->so_rcv.sb_lowat || \
  191. !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
  192. #define soreadable(so) \
  193. (soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
  194. /* can we write something to so? */
  195. #define sowriteable(so) \
  196. ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
  197. (((so)->so_state&SS_ISCONNECTED) || \
  198. ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
  199. ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
  200. (so)->so_error)
  201. /*
  202. * soref()/sorele() ref-count the socket structure. Note that you must
  203. * still explicitly close the socket, but the last ref count will free
  204. * the structure.
  205. */
  206. #define soref(so) do { \
  207. SOCK_LOCK_ASSERT(so); \
  208. ++(so)->so_count; \
  209. } while (0)
  210. #define sorele(so) do { \
  211. ACCEPT_LOCK_ASSERT(); \
  212. SOCK_LOCK_ASSERT(so); \
  213. if ((so)->so_count <= 0) \
  214. panic("sorele"); \
  215. if (--(so)->so_count == 0) \
  216. sofree(so); \
  217. else { \
  218. SOCK_UNLOCK(so); \
  219. ACCEPT_UNLOCK(); \
  220. } \
  221. } while (0)
  222. extern "C" void sowakeup(socket* so, sockbuf* sb);
  223. /*
  224. * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
  225. * avoid a non-atomic test-and-wakeup.
  226. */
  227. inline void sorwakeup_locked(socket* so) {
  228. SOCK_LOCK_ASSERT(so);
  229. if (sb_notify(&so->so_rcv)) {
  230. sowakeup(so, &so->so_rcv);
  231. }
  232. }
  233. inline void sorwakeup(socket* so)
  234. {
  235. SOCK_LOCK(so);
  236. sorwakeup_locked(so);
  237. SOCK_UNLOCK(so);
  238. }
  239. inline void sowwakeup_locked(socket* so)
  240. {
  241. SOCK_LOCK_ASSERT(so);
  242. if (sb_notify(&so->so_snd)) {
  243. sowakeup(so, &so->so_snd);
  244. }
  245. }
  246. inline void sowwakeup(socket* so)
  247. {
  248. SOCK_LOCK(so);
  249. sowwakeup_locked(so);
  250. SOCK_UNLOCK(so);
  251. }
  252. struct accept_filter {
  253. char accf_name[16];
  254. int (*accf_callback)
  255. (struct socket *so, void *arg, int waitflag);
  256. void * (*accf_create)
  257. (struct socket *so, char *arg);
  258. void (*accf_destroy)
  259. (struct socket *so);
  260. SLIST_ENTRY(accept_filter) accf_next;
  261. };
  262. #ifdef MALLOC_DECLARE
  263. MALLOC_DECLARE(M_ACCF);
  264. MALLOC_DECLARE(M_PCB);
  265. MALLOC_DECLARE(M_SONAME);
  266. #endif
  267. extern int maxsockets;
  268. extern u_long sb_max;
  269. extern struct uma_zone *socket_zone;
  270. extern so_gen_t so_gencnt;
  271. struct mbuf;
  272. struct bsd_sockaddr;
  273. struct ucred;
  274. struct uio;
  275. /* 'which' values for socket upcalls. */
  276. #define SO_RCV 1
  277. #define SO_SND 2
  278. /* Return values for socket upcalls. */
  279. #define SU_OK 0
  280. #define SU_ISCONNECTED 1
  281. __BEGIN_DECLS
  282. /*
  283. * From uipc_socket and friends
  284. */
  285. int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
  286. int getsockaddr(struct bsd_sockaddr **namp, caddr_t uaddr, size_t len);
  287. void soabort(struct socket *so);
  288. int soaccept(struct socket *so, struct bsd_sockaddr **nam);
  289. int socheckuid(struct socket *so, uid_t uid);
  290. int sobind(struct socket *so, struct bsd_sockaddr *nam, struct thread *td);
  291. int soclose(struct socket *so);
  292. int soconnect(struct socket *so, struct bsd_sockaddr *nam, struct thread *td);
  293. int soconnect2(struct socket *so1, struct socket *so2);
  294. int socow_setup(struct mbuf *m0, struct uio *uio);
  295. int socreate(int dom, struct socket **aso, int type, int proto,
  296. struct ucred *cred, struct thread *td);
  297. int sodisconnect(struct socket *so);
  298. struct bsd_sockaddr *sodupbsd_sockaddr(const struct bsd_sockaddr *sa, int mflags);
  299. void sofree(struct socket *so);
  300. void sohasoutofband(struct socket *so);
  301. int solisten(struct socket *so, int backlog, struct thread *td);
  302. void solisten_proto(struct socket *so, int backlog);
  303. int solisten_proto_check(struct socket *so);
  304. struct socket *
  305. sonewconn(struct socket *head, int connstatus);
  306. int sopoll(struct socket *so, int events, struct ucred *active_cred,
  307. struct thread *td);
  308. int sopoll_generic(struct socket *so, int events,
  309. struct ucred *active_cred, struct thread *td);
  310. int sopoll_generic_locked(struct socket *so, int events);
  311. int soreceive(struct socket *so, struct bsd_sockaddr **paddr, struct uio *uio,
  312. struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
  313. int soreceive_stream(struct socket *so, struct bsd_sockaddr **paddr,
  314. struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
  315. int *flagsp);
  316. int soreceive_dgram(struct socket *so, struct bsd_sockaddr **paddr,
  317. struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
  318. int *flagsp);
  319. int soreceive_generic(struct socket *so, struct bsd_sockaddr **paddr,
  320. struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
  321. int *flagsp);
  322. int zreceive(struct socket *so, struct bsd_sockaddr **paddr,
  323. struct zmsghdr *zm, int *flagsp, ssize_t *bytes);
  324. int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
  325. int soreserve_internal(struct socket *so, u_long sndcc, u_long rcvcc);
  326. void sorflush(struct socket *so);
  327. int sosend(struct socket *so, struct bsd_sockaddr *addr, struct uio *uio,
  328. struct mbuf *top, struct mbuf *control, int flags,
  329. struct thread *td);
  330. int sosend_dgram(struct socket *so, struct bsd_sockaddr *addr,
  331. struct uio *uio, struct mbuf *top, struct mbuf *control,
  332. int flags, struct thread *td);
  333. int sosend_generic(struct socket *so, struct bsd_sockaddr *addr,
  334. struct uio *uio, struct mbuf *top, struct mbuf *control,
  335. int flags, struct thread *td);
  336. int zsend(struct socket *so, struct uio *uio, struct zmsghdr *zm,
  337. int flags);
  338. int soshutdown(struct socket *so, int how);
  339. void sotoxsocket(struct socket *so, struct xsocket *xso);
  340. void soupcall_clear(struct socket *so, int which);
  341. void soupcall_set(struct socket *so, int which,
  342. int (*func)(struct socket *, void *, int), void *arg);
  343. void sowakeup(struct socket *so, struct sockbuf *sb);
  344. int selsocket(struct socket *so, int events, struct timeval *tv,
  345. struct thread *td);
  346. /*
  347. * Accept filter functions (duh).
  348. */
  349. int accept_filt_add(struct accept_filter *filt);
  350. int accept_filt_del(char *name);
  351. struct accept_filter *accept_filt_get(char *name);
  352. #ifdef ACCEPT_FILTER_MOD
  353. #ifdef SYSCTL_DECL
  354. SYSCTL_DECL(_net_inet_accf);
  355. #endif
  356. int accept_filt_generic_mod_event(module_t mod, int event, void *data);
  357. #endif
  358. __END_DECLS
  359. #endif /* _KERNEL */
  360. #endif /* !_SYS_SOCKETVAR_H_ */