Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * VMware vSockets Driver
0004  *
0005  * Copyright (C) 2007-2013 VMware, Inc. All rights reserved.
0006  */
0007 
0008 #ifndef __AF_VSOCK_H__
0009 #define __AF_VSOCK_H__
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/workqueue.h>
0013 #include <net/sock.h>
0014 #include <uapi/linux/vm_sockets.h>
0015 
0016 #include "vsock_addr.h"
0017 
0018 #define LAST_RESERVED_PORT 1023
0019 
0020 #define VSOCK_HASH_SIZE         251
0021 extern struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
0022 extern struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
0023 extern spinlock_t vsock_table_lock;
0024 
0025 #define vsock_sk(__sk)    ((struct vsock_sock *)__sk)
0026 #define sk_vsock(__vsk)   (&(__vsk)->sk)
0027 
0028 struct vsock_sock {
0029     /* sk must be the first member. */
0030     struct sock sk;
0031     const struct vsock_transport *transport;
0032     struct sockaddr_vm local_addr;
0033     struct sockaddr_vm remote_addr;
0034     /* Links for the global tables of bound and connected sockets. */
0035     struct list_head bound_table;
0036     struct list_head connected_table;
0037     /* Accessed without the socket lock held. This means it can never be
0038      * modified outsided of socket create or destruct.
0039      */
0040     bool trusted;
0041     bool cached_peer_allow_dgram;   /* Dgram communication allowed to
0042                      * cached peer?
0043                      */
0044     u32 cached_peer;  /* Context ID of last dgram destination check. */
0045     const struct cred *owner;
0046     /* Rest are SOCK_STREAM only. */
0047     long connect_timeout;
0048     /* Listening socket that this came from. */
0049     struct sock *listener;
0050     /* Used for pending list and accept queue during connection handshake.
0051      * The listening socket is the head for both lists.  Sockets created
0052      * for connection requests are placed in the pending list until they
0053      * are connected, at which point they are put in the accept queue list
0054      * so they can be accepted in accept().  If accept() cannot accept the
0055      * connection, it is marked as rejected so the cleanup function knows
0056      * to clean up the socket.
0057      */
0058     struct list_head pending_links;
0059     struct list_head accept_queue;
0060     bool rejected;
0061     struct delayed_work connect_work;
0062     struct delayed_work pending_work;
0063     struct delayed_work close_work;
0064     bool close_work_scheduled;
0065     u32 peer_shutdown;
0066     bool sent_request;
0067     bool ignore_connecting_rst;
0068 
0069     /* Protected by lock_sock(sk) */
0070     u64 buffer_size;
0071     u64 buffer_min_size;
0072     u64 buffer_max_size;
0073 
0074     /* Private to transport. */
0075     void *trans;
0076 };
0077 
0078 s64 vsock_stream_has_data(struct vsock_sock *vsk);
0079 s64 vsock_stream_has_space(struct vsock_sock *vsk);
0080 struct sock *vsock_create_connected(struct sock *parent);
0081 
0082 /**** TRANSPORT ****/
0083 
0084 struct vsock_transport_recv_notify_data {
0085     u64 data1; /* Transport-defined. */
0086     u64 data2; /* Transport-defined. */
0087     bool notify_on_block;
0088 };
0089 
0090 struct vsock_transport_send_notify_data {
0091     u64 data1; /* Transport-defined. */
0092     u64 data2; /* Transport-defined. */
0093 };
0094 
0095 /* Transport features flags */
0096 /* Transport provides host->guest communication */
0097 #define VSOCK_TRANSPORT_F_H2G       0x00000001
0098 /* Transport provides guest->host communication */
0099 #define VSOCK_TRANSPORT_F_G2H       0x00000002
0100 /* Transport provides DGRAM communication */
0101 #define VSOCK_TRANSPORT_F_DGRAM     0x00000004
0102 /* Transport provides local (loopback) communication */
0103 #define VSOCK_TRANSPORT_F_LOCAL     0x00000008
0104 
0105 struct vsock_transport {
0106     struct module *module;
0107 
0108     /* Initialize/tear-down socket. */
0109     int (*init)(struct vsock_sock *, struct vsock_sock *);
0110     void (*destruct)(struct vsock_sock *);
0111     void (*release)(struct vsock_sock *);
0112 
0113     /* Cancel all pending packets sent on vsock. */
0114     int (*cancel_pkt)(struct vsock_sock *vsk);
0115 
0116     /* Connections. */
0117     int (*connect)(struct vsock_sock *);
0118 
0119     /* DGRAM. */
0120     int (*dgram_bind)(struct vsock_sock *, struct sockaddr_vm *);
0121     int (*dgram_dequeue)(struct vsock_sock *vsk, struct msghdr *msg,
0122                  size_t len, int flags);
0123     int (*dgram_enqueue)(struct vsock_sock *, struct sockaddr_vm *,
0124                  struct msghdr *, size_t len);
0125     bool (*dgram_allow)(u32 cid, u32 port);
0126 
0127     /* STREAM. */
0128     /* TODO: stream_bind() */
0129     ssize_t (*stream_dequeue)(struct vsock_sock *, struct msghdr *,
0130                   size_t len, int flags);
0131     ssize_t (*stream_enqueue)(struct vsock_sock *, struct msghdr *,
0132                   size_t len);
0133     s64 (*stream_has_data)(struct vsock_sock *);
0134     s64 (*stream_has_space)(struct vsock_sock *);
0135     u64 (*stream_rcvhiwat)(struct vsock_sock *);
0136     bool (*stream_is_active)(struct vsock_sock *);
0137     bool (*stream_allow)(u32 cid, u32 port);
0138 
0139     /* SEQ_PACKET. */
0140     ssize_t (*seqpacket_dequeue)(struct vsock_sock *vsk, struct msghdr *msg,
0141                      int flags);
0142     int (*seqpacket_enqueue)(struct vsock_sock *vsk, struct msghdr *msg,
0143                  size_t len);
0144     bool (*seqpacket_allow)(u32 remote_cid);
0145     u32 (*seqpacket_has_data)(struct vsock_sock *vsk);
0146 
0147     /* Notification. */
0148     int (*notify_poll_in)(struct vsock_sock *, size_t, bool *);
0149     int (*notify_poll_out)(struct vsock_sock *, size_t, bool *);
0150     int (*notify_recv_init)(struct vsock_sock *, size_t,
0151         struct vsock_transport_recv_notify_data *);
0152     int (*notify_recv_pre_block)(struct vsock_sock *, size_t,
0153         struct vsock_transport_recv_notify_data *);
0154     int (*notify_recv_pre_dequeue)(struct vsock_sock *, size_t,
0155         struct vsock_transport_recv_notify_data *);
0156     int (*notify_recv_post_dequeue)(struct vsock_sock *, size_t,
0157         ssize_t, bool, struct vsock_transport_recv_notify_data *);
0158     int (*notify_send_init)(struct vsock_sock *,
0159         struct vsock_transport_send_notify_data *);
0160     int (*notify_send_pre_block)(struct vsock_sock *,
0161         struct vsock_transport_send_notify_data *);
0162     int (*notify_send_pre_enqueue)(struct vsock_sock *,
0163         struct vsock_transport_send_notify_data *);
0164     int (*notify_send_post_enqueue)(struct vsock_sock *, ssize_t,
0165         struct vsock_transport_send_notify_data *);
0166     /* sk_lock held by the caller */
0167     void (*notify_buffer_size)(struct vsock_sock *, u64 *);
0168 
0169     /* Shutdown. */
0170     int (*shutdown)(struct vsock_sock *, int);
0171 
0172     /* Addressing. */
0173     u32 (*get_local_cid)(void);
0174 };
0175 
0176 /**** CORE ****/
0177 
0178 int vsock_core_register(const struct vsock_transport *t, int features);
0179 void vsock_core_unregister(const struct vsock_transport *t);
0180 
0181 /* The transport may downcast this to access transport-specific functions */
0182 const struct vsock_transport *vsock_core_get_transport(struct vsock_sock *vsk);
0183 
0184 /**** UTILS ****/
0185 
0186 /* vsock_table_lock must be held */
0187 static inline bool __vsock_in_bound_table(struct vsock_sock *vsk)
0188 {
0189     return !list_empty(&vsk->bound_table);
0190 }
0191 
0192 /* vsock_table_lock must be held */
0193 static inline bool __vsock_in_connected_table(struct vsock_sock *vsk)
0194 {
0195     return !list_empty(&vsk->connected_table);
0196 }
0197 
0198 void vsock_release_pending(struct sock *pending);
0199 void vsock_add_pending(struct sock *listener, struct sock *pending);
0200 void vsock_remove_pending(struct sock *listener, struct sock *pending);
0201 void vsock_enqueue_accept(struct sock *listener, struct sock *connected);
0202 void vsock_insert_connected(struct vsock_sock *vsk);
0203 void vsock_remove_bound(struct vsock_sock *vsk);
0204 void vsock_remove_connected(struct vsock_sock *vsk);
0205 struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr);
0206 struct sock *vsock_find_connected_socket(struct sockaddr_vm *src,
0207                      struct sockaddr_vm *dst);
0208 void vsock_remove_sock(struct vsock_sock *vsk);
0209 void vsock_for_each_connected_socket(struct vsock_transport *transport,
0210                      void (*fn)(struct sock *sk));
0211 int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk);
0212 bool vsock_find_cid(unsigned int cid);
0213 
0214 /**** TAP ****/
0215 
0216 struct vsock_tap {
0217     struct net_device *dev;
0218     struct module *module;
0219     struct list_head list;
0220 };
0221 
0222 int vsock_init_tap(void);
0223 int vsock_add_tap(struct vsock_tap *vt);
0224 int vsock_remove_tap(struct vsock_tap *vt);
0225 void vsock_deliver_tap(struct sk_buff *build_skb(void *opaque), void *opaque);
0226 
0227 #endif /* __AF_VSOCK_H__ */