047ecf9e38efb9a0b820aa007637c61bbec21910
[dpdk.git] / lib / librte_vhost / vhost-net-cdev.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _VHOST_NET_CDEV_H_
35 #define _VHOST_NET_CDEV_H_
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <linux/vhost.h>
41
42 #include <rte_log.h>
43
44 /* Macros for printing using RTE_LOG */
45 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
46 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
47
48 #ifdef RTE_LIBRTE_VHOST_DEBUG
49 #define VHOST_MAX_PRINT_BUFF 6072
50 #define LOG_LEVEL RTE_LOG_DEBUG
51 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
52 #define PRINT_PACKET(device, addr, size, header) do { \
53         char *pkt_addr = (char *)(addr); \
54         unsigned int index; \
55         char packet[VHOST_MAX_PRINT_BUFF]; \
56         \
57         if ((header)) \
58                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%"PRIu64") Header size %d: ", (device->device_fh), (size)); \
59         else \
60                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%"PRIu64") Packet size %d: ", (device->device_fh), (size)); \
61         for (index = 0; index < (size); index++) { \
62                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
63                         "%02hhx ", pkt_addr[index]); \
64         } \
65         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
66         \
67         LOG_DEBUG(VHOST_DATA, "%s", packet); \
68 } while (0)
69 #else
70 #define LOG_LEVEL RTE_LOG_INFO
71 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
72 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
73 #endif
74
75
76 /*
77  * Structure used to identify device context.
78  */
79 struct vhost_device_ctx
80 {
81         pid_t           pid;    /* PID of process calling the IOCTL. */
82         uint64_t        fh;             /* Populated with fi->fh to track the device index. */
83 };
84
85 /*
86  * Structure contains function pointers to be defined in virtio-net.c. These
87  * functions are called in CUSE context and are used to configure devices.
88  */
89 struct vhost_net_device_ops {
90         int (* new_device)              (struct vhost_device_ctx);
91         void (* destroy_device) (struct vhost_device_ctx);
92
93         int (* get_features)    (struct vhost_device_ctx, uint64_t *);
94         int (* set_features)    (struct vhost_device_ctx, uint64_t *);
95
96         int (* set_mem_table)   (struct vhost_device_ctx, const void *, uint32_t);
97
98         int (* set_vring_num)   (struct vhost_device_ctx, struct vhost_vring_state *);
99         int (* set_vring_addr)  (struct vhost_device_ctx, struct vhost_vring_addr *);
100         int (* set_vring_base)  (struct vhost_device_ctx, struct vhost_vring_state *);
101         int (* get_vring_base)  (struct vhost_device_ctx, uint32_t, struct vhost_vring_state *);
102
103         int (* set_vring_kick)  (struct vhost_device_ctx, struct vhost_vring_file *);
104         int (* set_vring_call)  (struct vhost_device_ctx, struct vhost_vring_file *);
105
106         int (* set_backend)     (struct vhost_device_ctx, struct vhost_vring_file *);
107
108         int (* set_owner)               (struct vhost_device_ctx);
109         int (* reset_owner)     (struct vhost_device_ctx);
110 };
111
112
113 struct vhost_net_device_ops const *get_virtio_net_callbacks(void);
114 #endif /* _VHOST_NET_CDEV_H_ */