18389fb91SDamjan Marion/*
28389fb91SDamjan Marion *------------------------------------------------------------------
38389fb91SDamjan Marion * Copyright (c) 2017 Cisco and/or its affiliates.
48389fb91SDamjan Marion * Licensed under the Apache License, Version 2.0 (the "License");
58389fb91SDamjan Marion * you may not use this file except in compliance with the License.
68389fb91SDamjan Marion * You may obtain a copy of the License at:
78389fb91SDamjan Marion *
88389fb91SDamjan Marion *     http://www.apache.org/licenses/LICENSE-2.0
98389fb91SDamjan Marion *
108389fb91SDamjan Marion * Unless required by applicable law or agreed to in writing, software
118389fb91SDamjan Marion * distributed under the License is distributed on an "AS IS" BASIS,
128389fb91SDamjan Marion * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138389fb91SDamjan Marion * See the License for the specific language governing permissions and
148389fb91SDamjan Marion * limitations under the License.
158389fb91SDamjan Marion *------------------------------------------------------------------
168389fb91SDamjan Marion */
178389fb91SDamjan Marion
188389fb91SDamjan Marion#ifndef _VNET_DEVICES_VIRTIO_VIRTIO_H_
198389fb91SDamjan Marion#define _VNET_DEVICES_VIRTIO_VIRTIO_H_
208389fb91SDamjan Marion
21d6c15af3SMohsin Kazmi#include <linux/virtio_config.h>
22d6c15af3SMohsin Kazmi#include <linux/virtio_net.h>
23d6c15af3SMohsin Kazmi#include <linux/virtio_pci.h>
24d6c15af3SMohsin Kazmi#include <linux/virtio_ring.h>
25d6c15af3SMohsin Kazmi
268389fb91SDamjan Marion#define foreach_virtio_net_features      \
278389fb91SDamjan Marion  _ (VIRTIO_NET_F_CSUM, 0)	/* Host handles pkts w/ partial csum */ \
288389fb91SDamjan Marion  _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \
298389fb91SDamjan Marion  _ (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 2) /* Dynamic offload configuration. */ \
308389fb91SDamjan Marion  _ (VIRTIO_NET_F_MTU, 3)       /* Initial MTU advice. */ \
318389fb91SDamjan Marion  _ (VIRTIO_NET_F_MAC, 5)	/* Host has given MAC address. */ \
328389fb91SDamjan Marion  _ (VIRTIO_NET_F_GSO, 6)	/* Host handles pkts w/ any GSO. */ \
338389fb91SDamjan Marion  _ (VIRTIO_NET_F_GUEST_TSO4, 7)	/* Guest can handle TSOv4 in. */ \
348389fb91SDamjan Marion  _ (VIRTIO_NET_F_GUEST_TSO6, 8)	/* Guest can handle TSOv6 in. */ \
358389fb91SDamjan Marion  _ (VIRTIO_NET_F_GUEST_ECN, 9)	/* Guest can handle TSO[6] w/ ECN in. */ \
368389fb91SDamjan Marion  _ (VIRTIO_NET_F_GUEST_UFO, 10)	/* Guest can handle UFO in. */ \
378389fb91SDamjan Marion  _ (VIRTIO_NET_F_HOST_TSO4, 11)	/* Host can handle TSOv4 in. */ \
388389fb91SDamjan Marion  _ (VIRTIO_NET_F_HOST_TSO6, 12)	/* Host can handle TSOv6 in. */ \
398389fb91SDamjan Marion  _ (VIRTIO_NET_F_HOST_ECN, 13)	/* Host can handle TSO[6] w/ ECN in. */ \
408389fb91SDamjan Marion  _ (VIRTIO_NET_F_HOST_UFO, 14)	/* Host can handle UFO in. */ \
418389fb91SDamjan Marion  _ (VIRTIO_NET_F_MRG_RXBUF, 15)	/* Host can merge receive buffers. */ \
428389fb91SDamjan Marion  _ (VIRTIO_NET_F_STATUS, 16)	/* virtio_net_config.status available */ \
438389fb91SDamjan Marion  _ (VIRTIO_NET_F_CTRL_VQ, 17)	/* Control channel available */ \
448389fb91SDamjan Marion  _ (VIRTIO_NET_F_CTRL_RX, 18)	/* Control channel RX mode support */ \
458389fb91SDamjan Marion  _ (VIRTIO_NET_F_CTRL_VLAN, 19)	/* Control channel VLAN filtering */ \
468389fb91SDamjan Marion  _ (VIRTIO_NET_F_CTRL_RX_EXTRA, 20)	/* Extra RX mode control support */ \
478389fb91SDamjan Marion  _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21)	/* Guest can announce device on the network */ \
488389fb91SDamjan Marion  _ (VIRTIO_NET_F_MQ, 22)	        /* Device supports Receive Flow Steering */ \
498389fb91SDamjan Marion  _ (VIRTIO_NET_F_CTRL_MAC_ADDR, 23)	/* Set MAC address */ \
508389fb91SDamjan Marion  _ (VIRTIO_F_NOTIFY_ON_EMPTY, 24) \
518389fb91SDamjan Marion  _ (VHOST_F_LOG_ALL, 26)      /* Log all write descriptors */ \
5297c998c2SPaul Vinciguerra  _ (VIRTIO_F_ANY_LAYOUT, 27)  /* Can the device handle any descriptor layout */ \
538389fb91SDamjan Marion  _ (VIRTIO_RING_F_INDIRECT_DESC, 28)   /* Support indirect buffer descriptors */ \
548389fb91SDamjan Marion  _ (VIRTIO_RING_F_EVENT_IDX, 29)       /* The Guest publishes the used index for which it expects an interrupt \
558389fb91SDamjan Marion * at the end of the avail ring. Host should ignore the avail->flags field. */ \
568389fb91SDamjan Marion/* The Host publishes the avail index for which it expects a kick \
578389fb91SDamjan Marion * at the end of the used ring. Guest should ignore the used->flags field. */ \
588389fb91SDamjan Marion  _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
598389fb91SDamjan Marion  _ (VIRTIO_F_VERSION_1, 32)
608389fb91SDamjan Marion
618389fb91SDamjan Marion#define foreach_virtio_if_flag		\
628389fb91SDamjan Marion  _(0, ADMIN_UP, "admin-up")		\
638389fb91SDamjan Marion  _(1, DELETING, "deleting")
648389fb91SDamjan Marion
658389fb91SDamjan Mariontypedef enum
668389fb91SDamjan Marion{
678389fb91SDamjan Marion#define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
688389fb91SDamjan Marion  foreach_virtio_if_flag
698389fb91SDamjan Marion#undef _
708389fb91SDamjan Marion} virtio_if_flag_t;
718389fb91SDamjan Marion
72d6c15af3SMohsin Kazmi#define VIRTIO_NUM_RX_DESC 256
73d6c15af3SMohsin Kazmi#define VIRTIO_NUM_TX_DESC 256
74d6c15af3SMohsin Kazmi
75d6c15af3SMohsin Kazmi#define VIRTIO_FEATURE(X) (1ULL << X)
76d6c15af3SMohsin Kazmi
7709a3bc50SMohsin Kazmi#define TX_QUEUE(X) ((X*2) + 1)
7809a3bc50SMohsin Kazmi#define RX_QUEUE(X) (X*2)
7909a3bc50SMohsin Kazmi#define TX_QUEUE_ACCESS(X) (X/2)
8009a3bc50SMohsin Kazmi#define RX_QUEUE_ACCESS(X) (X/2)
8109a3bc50SMohsin Kazmi
82206acf84SMohsin Kazmi#define foreach_virtio_if_types \
83d88fc0fcSMohsin Kazmi  _ (TAP, 0)                    \
84d88fc0fcSMohsin Kazmi  _ (TUN, 1)                    \
85d88fc0fcSMohsin Kazmi  _ (PCI, 2)
86206acf84SMohsin Kazmi
878389fb91SDamjan Mariontypedef enum
888389fb91SDamjan Marion{
89206acf84SMohsin Kazmi#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
90206acf84SMohsin Kazmi  foreach_virtio_if_types
91206acf84SMohsin Kazmi#undef _
92d88fc0fcSMohsin Kazmi    VIRTIO_IF_N_TYPES = (1 << 3),
938389fb91SDamjan Marion} virtio_if_type_t;
948389fb91SDamjan Marion
958389fb91SDamjan Marion
96d6c15af3SMohsin Kazmitypedef struct
97d6c15af3SMohsin Kazmi{
98d6c15af3SMohsin Kazmi  u8 mac[6];
99d6c15af3SMohsin Kazmi  u16 status;
100d6c15af3SMohsin Kazmi  u16 max_virtqueue_pairs;
101d6c15af3SMohsin Kazmi  u16 mtu;
102d6c15af3SMohsin Kazmi} virtio_net_config_t;
103d6c15af3SMohsin Kazmi
104d6c15af3SMohsin Kazmi#define VIRTIO_RING_FLAG_MASK_INT 1
105d6c15af3SMohsin Kazmi
1068389fb91SDamjan Mariontypedef struct
1078389fb91SDamjan Marion{
108d9818dd6SDavid Johnson  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
1098389fb91SDamjan Marion  struct vring_desc *desc;
1108389fb91SDamjan Marion  struct vring_used *used;
1118389fb91SDamjan Marion  struct vring_avail *avail;
11209a3bc50SMohsin Kazmi  clib_spinlock_t lockp;
1138389fb91SDamjan Marion  u16 desc_in_use;
1148389fb91SDamjan Marion  u16 desc_next;
1158389fb91SDamjan Marion  int kick_fd;
1168389fb91SDamjan Marion  int call_fd;
11780659b46SMohsin Kazmi  u8 buffer_pool_index;
1188389fb91SDamjan Marion  u16 size;
119d6c15af3SMohsin Kazmi  u16 queue_id;
1207f6d145aSMohsin Kazmi#define VRING_TX_OUT_OF_ORDER 1
121d6c15af3SMohsin Kazmi  u16 flags;
1228389fb91SDamjan Marion  u32 call_file_index;
1238389fb91SDamjan Marion  u32 *buffers;
1248389fb91SDamjan Marion  u16 last_used_idx;
125e40231b1SDamjan Marion  u16 last_kick_avail_idx;
1268389fb91SDamjan Marion} virtio_vring_t;
1278389fb91SDamjan Marion
128d6c15af3SMohsin Kazmitypedef union
129d6c15af3SMohsin Kazmi{
130d6c15af3SMohsin Kazmi  struct
131d6c15af3SMohsin Kazmi  {
132d6c15af3SMohsin Kazmi    u16 domain;
133d6c15af3SMohsin Kazmi    u8 bus;
134d6c15af3SMohsin Kazmi    u8 slot:5;
135d6c15af3SMohsin Kazmi    u8 function:3;
136d6c15af3SMohsin Kazmi  };
137d6c15af3SMohsin Kazmi  u32 as_u32;
138d6c15af3SMohsin Kazmi} pci_addr_t;
139d6c15af3SMohsin Kazmi
1408389fb91SDamjan Mariontypedef struct
1418389fb91SDamjan Marion{
142d6c15af3SMohsin Kazmi  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
1438389fb91SDamjan Marion  u32 flags;
144829ee539SDamjan Marion
1458389fb91SDamjan Marion  u32 dev_instance;
1468389fb91SDamjan Marion  u32 hw_if_index;
1478389fb91SDamjan Marion  u32 sw_if_index;
14809a3bc50SMohsin Kazmi  u32 numa_node;
149d6c15af3SMohsin Kazmi  u16 virtio_net_hdr_sz;
150d6c15af3SMohsin Kazmi  virtio_if_type_t type;
151d6c15af3SMohsin Kazmi  union
152d6c15af3SMohsin Kazmi  {
153d6c15af3SMohsin Kazmi    u32 id;
154d6c15af3SMohsin Kazmi    pci_addr_t pci_addr;
155d6c15af3SMohsin Kazmi  };
1568389fb91SDamjan Marion  u32 per_interface_next_index;
1577c6102b1SDamjan Marion  int *vhost_fds;
1582857e785SAloys Augustin  int *tap_fds;
1597c6102b1SDamjan Marion  u32 msix_enabled;
1607c6102b1SDamjan Marion  u32 pci_dev_handle;
16109a3bc50SMohsin Kazmi  virtio_vring_t *rxq_vrings;
16209a3bc50SMohsin Kazmi  virtio_vring_t *txq_vrings;
1638389fb91SDamjan Marion  u64 features, remote_features;
1648389fb91SDamjan Marion
165d6c15af3SMohsin Kazmi  /* error */
166d6c15af3SMohsin Kazmi  clib_error_t *error;
167b74fe32dSMohsin Kazmi  u8 support_int_mode;		/* support interrupt mode */
168d6c15af3SMohsin Kazmi  u16 max_queue_pairs;
16909a3bc50SMohsin Kazmi  u16 num_rxqs;
17009a3bc50SMohsin Kazmi  u16 num_txqs;
171d6c15af3SMohsin Kazmi  u8 status;
172d6c15af3SMohsin Kazmi  u8 mac_addr[6];
1732df39094SDamjan Marion  u8 *host_if_name;
1748389fb91SDamjan Marion  u8 *net_ns;
17573e7f427SMilan Lenco  u8 *host_bridge;
17673e7f427SMilan Lenco  u8 host_mac_addr[6];
17773e7f427SMilan Lenco  ip4_address_t host_ip4_addr;
17873e7f427SMilan Lenco  u8 host_ip4_prefix_len;
17973e7f427SMilan Lenco  ip6_address_t host_ip6_addr;
18073e7f427SMilan Lenco  u8 host_ip6_prefix_len;
18197d54ed4SMohsin Kazmi  u32 host_mtu_size;
1826a7cff7eSAndrew Yourtchenko  int gso_enabled;
1836d4af891SMohsin Kazmi  int csum_offload_enabled;
1848389fb91SDamjan Marion  int ifindex;
18509a3bc50SMohsin Kazmi  virtio_vring_t *cxq_vring;
1868389fb91SDamjan Marion} virtio_if_t;
1878389fb91SDamjan Marion
1888389fb91SDamjan Mariontypedef struct
1898389fb91SDamjan Marion{
19033cc5cfdSMohsin Kazmi  /* logging */
19133cc5cfdSMohsin Kazmi  vlib_log_class_t log_default;
19233cc5cfdSMohsin Kazmi
1938389fb91SDamjan Marion  virtio_if_t *interfaces;
1948389fb91SDamjan Marion} virtio_main_t;
1958389fb91SDamjan Marion
1968389fb91SDamjan Marionextern virtio_main_t virtio_main;
1978389fb91SDamjan Marionextern vnet_device_class_t virtio_device_class;
1988389fb91SDamjan Marionextern vlib_node_registration_t virtio_input_node;
1998389fb91SDamjan Marion
2008389fb91SDamjan Marionclib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
2018389fb91SDamjan Marion				 u16 sz);
20209a3bc50SMohsin Kazmiclib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
20309a3bc50SMohsin Kazmi				    u32 idx);
20409a3bc50SMohsin Kazmiclib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
20509a3bc50SMohsin Kazmi				    u32 idx);
20680659b46SMohsin Kazmivoid virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
207a624dbe7SSteven				 u32 idx);
208a624dbe7SStevenextern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
209d6c15af3SMohsin Kazmiextern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
210d6c15af3SMohsin Kazmiextern void virtio_set_net_hdr_size (virtio_if_t * vif);
211d6c15af3SMohsin Kazmiextern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
212d6c15af3SMohsin Kazmi			 u32 type);
213d6c15af3SMohsin Kazmiextern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
214d6c15af3SMohsin Kazmi					    virtio_if_t * vif, u16 queue_id);
215ceab7882SDamjan Marionformat_function_t format_virtio_device_name;
216f41244f4SDamjan Marionformat_function_t format_virtio_log_name;
217ceab7882SDamjan Marion
218e40231b1SDamjan Marionstatic_always_inline void
219d6c15af3SMohsin Kazmivirtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
220e40231b1SDamjan Marion{
221d6c15af3SMohsin Kazmi  if (vif->type == VIRTIO_IF_TYPE_PCI)
222d6c15af3SMohsin Kazmi    virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
223d6c15af3SMohsin Kazmi  else
224d6c15af3SMohsin Kazmi    {
225d6c15af3SMohsin Kazmi      u64 x = 1;
226d6c15af3SMohsin Kazmi      int __clib_unused r;
227d6c15af3SMohsin Kazmi
228d6c15af3SMohsin Kazmi      r = write (vring->kick_fd, &x, sizeof (x));
229d6c15af3SMohsin Kazmi      vring->last_kick_avail_idx = vring->avail->idx;
230d6c15af3SMohsin Kazmi    }
231e40231b1SDamjan Marion}
232e40231b1SDamjan Marion
233f41244f4SDamjan Marion
234f41244f4SDamjan Marion#define virtio_log_debug(vif, f, ...)				\
235f41244f4SDamjan Marion{								\
236f41244f4SDamjan Marion  vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default,	\
237f41244f4SDamjan Marion	   "%U: " f, format_virtio_log_name, vif,		\
238f41244f4SDamjan Marion           ##__VA_ARGS__);					\
239f41244f4SDamjan Marion};
240f41244f4SDamjan Marion
241f41244f4SDamjan Marion#define virtio_log_warning(vif, f, ...)				\
242f41244f4SDamjan Marion{								\
243f41244f4SDamjan Marion  vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default,	\
244f41244f4SDamjan Marion	   "%U: " f, format_virtio_log_name, vif,		\
245f41244f4SDamjan Marion           ##__VA_ARGS__);					\
246f41244f4SDamjan Marion};
247f41244f4SDamjan Marion
248f41244f4SDamjan Marion#define virtio_log_error(vif, f, ...)				\
249f41244f4SDamjan Marion{								\
250f41244f4SDamjan Marion  vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default,		\
251f41244f4SDamjan Marion	   "%U: " f, format_virtio_log_name, vif,		\
252f41244f4SDamjan Marion           ##__VA_ARGS__);					\
253f41244f4SDamjan Marion};
254f41244f4SDamjan Marion
2558389fb91SDamjan Marion#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
2568389fb91SDamjan Marion
2578389fb91SDamjan Marion/*
2588389fb91SDamjan Marion * fd.io coding-style-patch-verification: ON
2598389fb91SDamjan Marion *
2608389fb91SDamjan Marion * Local Variables:
2618389fb91SDamjan Marion * eval: (c-set-style "gnu")
2628389fb91SDamjan Marion * End:
2638389fb91SDamjan Marion */
264