CNDP  22.08.0
pmd_memif_socket.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018-2021 Cisco Systems, Inc. All rights reserved.
3  */
4 
5 #ifndef _CNE_ETH_MEMIF_H_
6 #define _CNE_ETH_MEMIF_H_
7 
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif /* GNU_SOURCE */
11 
12 #include <sys/queue.h>
13 
14 #include <cne_spinlock.h>
15 #include <cne_event.h>
16 #include "pktdev_api.h" // for pktdev_get_name_by_port, pktdev_portid
17 #include "pktdev_core.h" // for cne_pktdev, pktdev_data, pktdev_ops
18 #include "netdev_funcs.h" // for netdev_get_mac_addr
19 #include "memif_socket.h"
20 
21 #define PMD_NET_MEMIF_NAME "net_memif_socket"
22 
23 #define CNE_ETH_MEMIF_DEFAULT_SOCKET_FILENAME "/var/run/memif.sock"
24 #define CNE_ETH_MEMIF_DEFAULT_RING_SIZE 10
25 #define CNE_ETH_MEMIF_DEFAULT_PKT_BUFFER_SIZE 2048
26 
27 #define CNE_ETH_MEMIF_MAX_NUM_Q_PAIRS 1
28 #define CNE_ETH_MEMIF_MAX_LOG2_RING_SIZE 14
29 #define CNE_ETH_MEMIF_MAX_REGION_NUM 256
30 
31 #define CNE_ETH_MEMIF_SHM_NAME_SIZE 32
32 #define CNE_ETH_MEMIF_DISC_STRING_SIZE 96
33 #define CNE_ETH_MEMIF_SECRET_SIZE 24
34 
35 extern int cne_memif_logtype;
36 
37 #define MIF_LOG(level, fmt, args...) cne_log(CNE_LOG_##level, __func__, __LINE__, fmt, ##args)
38 
39 enum cne_memif_role_t {
40  CNE_MEMIF_ROLE_SERVER,
41  CNE_MEMIF_ROLE_CLIENT,
42 };
43 
44 struct cne_memif_region {
45  void *addr;
46  cne_memif_region_size_t region_size;
47  int fd;
48  uint32_t pkt_buffer_offset;
50 };
51 
52 struct pmd_internals {
53  cne_memif_interface_id_t id;
54  enum cne_memif_role_t role;
55  uint32_t flags;
56 #define CNE_ETH_MEMIF_FLAG_CONNECTING (1 << 0)
58 #define CNE_ETH_MEMIF_FLAG_CONNECTED (1 << 1)
60 #define CNE_ETH_MEMIF_FLAG_ZERO_COPY (1 << 2)
62 #define CNE_ETH_MEMIF_FLAG_DISABLED (1 << 3)
64 #define CNE_ETH_MEMIF_FLAG_SOCKET_ABSTRACT (1 << 4)
67  char *socket_filename;
68  struct cne_memif_socket *socket;
69  char secret[CNE_ETH_MEMIF_SECRET_SIZE];
70  pktmbuf_info_t *pi;
71  struct cne_memif_control_channel *cc;
72  cne_spinlock_t cc_lock;
74  /* remote info */
75  char remote_name[PKTDEV_NAME_MAX_LEN];
76  char remote_if_name[PKTDEV_NAME_MAX_LEN];
77  char pmd_name[PKTDEV_NAME_MAX_LEN];
78 
79  struct {
80  cne_memif_log2_ring_size_t log2_ring_size;
81  uint8_t num_c2s_rings;
82  uint8_t num_s2c_rings;
83  uint16_t pkt_buffer_size;
84  } cfg;
86  struct {
87  cne_memif_log2_ring_size_t log2_ring_size;
88  uint8_t num_c2s_rings;
89  uint8_t num_s2c_rings;
90  uint16_t pkt_buffer_size;
91  } run;
94  char local_disc_string[CNE_ETH_MEMIF_DISC_STRING_SIZE];
96  char remote_disc_string[CNE_ETH_MEMIF_DISC_STRING_SIZE];
98 };
99 
100 struct cne_memif_queue {
101  pktmbuf_info_t *pi;
102  struct pmd_internals *pmd;
104  cne_memif_ring_type_t type;
105  cne_memif_region_index_t region;
107  uint16_t in_port;
109  cne_memif_region_offset_t ring_offset;
112  uint16_t last_head;
113  uint16_t last_tail;
115  struct cne_mbuf **buffers;
120  /* rx/tx info */
121  uint64_t n_pkts;
122  uint64_t n_bytes;
124  struct cne_ev_handle ev_handle;
126  cne_memif_log2_ring_size_t log2_ring_size;
127 };
128 
129 struct pmd_process_private {
130  struct cne_memif_region *regions[CNE_ETH_MEMIF_MAX_REGION_NUM];
132  cne_memif_region_index_t regions_num;
133 };
134 
141 void cne_memif_free_regions(struct cne_pktdev *dev);
142 
153 int cne_memif_connect(struct cne_pktdev *dev);
154 
155 int cne_memif_connect_start(struct cne_pktdev *dev);
156 
167 int cne_memif_init_regions_and_queues(struct cne_pktdev *dev);
168 
175 const char *cne_memif_version(void);
176 
177 #ifndef MFD_HUGETLB
178 #ifndef __NR_memfd_create
179 
180 #if defined __x86_64__
181 #define __NR_memfd_create 319
182 #else
183 #error "__NR_memfd_create unknown for this architecture"
184 #endif
185 
186 #endif /* __NR_memfd_create */
187 
188 static inline int
189 memfd_create(const char *name, unsigned int flags)
190 {
191  return syscall(__NR_memfd_create, name, flags);
192 }
193 #endif /* MFD_HUGETLB */
194 
195 #ifndef F_LINUX_SPECIFIC_BASE
196 #define F_LINUX_SPECIFIC_BASE 1024
197 #endif
198 
199 #ifndef MFD_ALLOW_SEALING
200 #define MFD_ALLOW_SEALING 0x0002U
201 #endif
202 
203 #ifndef F_ADD_SEALS
204 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
205 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
206 
207 #define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
208 #define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
209 #define F_SEAL_GROW 0x0004 /* prevent file from growing */
210 #define F_SEAL_WRITE 0x0008 /* prevent writes */
211 #endif
212 
213 #endif /* CNE_ETH_MEMIF_H */