2
0

net.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #ifndef QEMU_NET_H
  2. #define QEMU_NET_H
  3. #include "qemu/queue.h"
  4. #include "qapi/qapi-types-net.h"
  5. #include "net/queue.h"
  6. #include "hw/qdev-properties-system.h"
  7. #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
  8. #define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \
  9. ((uint8_t *)(x))[2], ((uint8_t *)(x))[3], \
  10. ((uint8_t *)(x))[4], ((uint8_t *)(x))[5]
  11. #define MAX_QUEUE_NUM 1024
  12. /* Maximum GSO packet size (64k) plus plenty of room for
  13. * the ethernet and virtio_net headers
  14. */
  15. #define NET_BUFSIZE (4096 + 65536)
  16. struct MACAddr {
  17. uint8_t a[6];
  18. };
  19. /* qdev nic properties */
  20. typedef struct NICPeers {
  21. NetClientState *ncs[MAX_QUEUE_NUM];
  22. int32_t queues;
  23. } NICPeers;
  24. typedef struct NICConf {
  25. MACAddr macaddr;
  26. NICPeers peers;
  27. int32_t bootindex;
  28. } NICConf;
  29. #define DEFINE_NIC_PROPERTIES(_state, _conf) \
  30. DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \
  31. DEFINE_PROP_NETDEV("netdev", _state, _conf.peers)
  32. /* Net clients */
  33. typedef void (NetPoll)(NetClientState *, bool enable);
  34. typedef bool (NetCanReceive)(NetClientState *);
  35. typedef int (NetStart)(NetClientState *);
  36. typedef int (NetLoad)(NetClientState *);
  37. typedef void (NetStop)(NetClientState *);
  38. typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
  39. typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
  40. typedef void (NetCleanup) (NetClientState *);
  41. typedef void (LinkStatusChanged)(NetClientState *);
  42. typedef void (NetClientDestructor)(NetClientState *);
  43. typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
  44. typedef bool (HasUfo)(NetClientState *);
  45. typedef bool (HasUso)(NetClientState *);
  46. typedef bool (HasVnetHdr)(NetClientState *);
  47. typedef bool (HasVnetHdrLen)(NetClientState *, int);
  48. typedef void (SetOffload)(NetClientState *, int, int, int, int, int, int, int);
  49. typedef int (GetVnetHdrLen)(NetClientState *);
  50. typedef void (SetVnetHdrLen)(NetClientState *, int);
  51. typedef int (SetVnetLE)(NetClientState *, bool);
  52. typedef int (SetVnetBE)(NetClientState *, bool);
  53. typedef struct SocketReadState SocketReadState;
  54. typedef void (SocketReadStateFinalize)(SocketReadState *rs);
  55. typedef void (NetAnnounce)(NetClientState *);
  56. typedef bool (SetSteeringEBPF)(NetClientState *, int);
  57. typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
  58. typedef struct NetClientInfo {
  59. NetClientDriver type;
  60. size_t size;
  61. NetReceive *receive;
  62. NetReceiveIOV *receive_iov;
  63. NetCanReceive *can_receive;
  64. NetStart *start;
  65. NetLoad *load;
  66. NetStop *stop;
  67. NetCleanup *cleanup;
  68. LinkStatusChanged *link_status_changed;
  69. QueryRxFilter *query_rx_filter;
  70. NetPoll *poll;
  71. HasUfo *has_ufo;
  72. HasUso *has_uso;
  73. HasVnetHdr *has_vnet_hdr;
  74. HasVnetHdrLen *has_vnet_hdr_len;
  75. SetOffload *set_offload;
  76. SetVnetHdrLen *set_vnet_hdr_len;
  77. SetVnetLE *set_vnet_le;
  78. SetVnetBE *set_vnet_be;
  79. NetAnnounce *announce;
  80. SetSteeringEBPF *set_steering_ebpf;
  81. NetCheckPeerType *check_peer_type;
  82. } NetClientInfo;
  83. struct NetClientState {
  84. NetClientInfo *info;
  85. int link_down;
  86. QTAILQ_ENTRY(NetClientState) next;
  87. NetClientState *peer;
  88. NetQueue *incoming_queue;
  89. char *model;
  90. char *name;
  91. char info_str[256];
  92. unsigned receive_disabled : 1;
  93. NetClientDestructor *destructor;
  94. unsigned int queue_index;
  95. unsigned rxfilter_notify_enabled:1;
  96. int vring_enable;
  97. int vnet_hdr_len;
  98. bool is_netdev;
  99. bool do_not_pad; /* do not pad to the minimum ethernet frame length */
  100. bool is_datapath;
  101. QTAILQ_HEAD(, NetFilterState) filters;
  102. };
  103. typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList;
  104. typedef struct NICState {
  105. NetClientState *ncs;
  106. NICConf *conf;
  107. MemReentrancyGuard *reentrancy_guard;
  108. void *opaque;
  109. bool peer_deleted;
  110. } NICState;
  111. struct SocketReadState {
  112. /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
  113. int state;
  114. /* This flag decide whether to read the vnet_hdr_len field */
  115. bool vnet_hdr;
  116. uint32_t index;
  117. uint32_t packet_len;
  118. uint32_t vnet_hdr_len;
  119. uint8_t buf[NET_BUFSIZE];
  120. SocketReadStateFinalize *finalize;
  121. };
  122. int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
  123. char *qemu_mac_strdup_printf(const uint8_t *macaddr);
  124. NetClientState *qemu_find_netdev(const char *id);
  125. int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
  126. NetClientDriver type, int max);
  127. NetClientState *qemu_new_net_client(NetClientInfo *info,
  128. NetClientState *peer,
  129. const char *model,
  130. const char *name);
  131. NetClientState *qemu_new_net_control_client(NetClientInfo *info,
  132. NetClientState *peer,
  133. const char *model,
  134. const char *name);
  135. NICState *qemu_new_nic(NetClientInfo *info,
  136. NICConf *conf,
  137. const char *model,
  138. const char *name,
  139. MemReentrancyGuard *reentrancy_guard,
  140. void *opaque);
  141. void qemu_del_nic(NICState *nic);
  142. NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
  143. NetClientState *qemu_get_queue(NICState *nic);
  144. NICState *qemu_get_nic(NetClientState *nc);
  145. void *qemu_get_nic_opaque(NetClientState *nc);
  146. void qemu_del_net_client(NetClientState *nc);
  147. typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
  148. void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
  149. int qemu_can_receive_packet(NetClientState *nc);
  150. int qemu_can_send_packet(NetClientState *nc);
  151. ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
  152. int iovcnt);
  153. ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
  154. int iovcnt, NetPacketSent *sent_cb);
  155. ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
  156. ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size);
  157. ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
  158. ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
  159. int size, NetPacketSent *sent_cb);
  160. void qemu_purge_queued_packets(NetClientState *nc);
  161. void qemu_flush_queued_packets(NetClientState *nc);
  162. void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
  163. void qemu_set_info_str(NetClientState *nc,
  164. const char *fmt, ...) G_GNUC_PRINTF(2, 3);
  165. void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
  166. bool qemu_has_ufo(NetClientState *nc);
  167. bool qemu_has_uso(NetClientState *nc);
  168. bool qemu_has_vnet_hdr(NetClientState *nc);
  169. bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
  170. void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  171. int ecn, int ufo, int uso4, int uso6);
  172. int qemu_get_vnet_hdr_len(NetClientState *nc);
  173. void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
  174. int qemu_set_vnet_le(NetClientState *nc, bool is_le);
  175. int qemu_set_vnet_be(NetClientState *nc, bool is_be);
  176. void qemu_macaddr_default_if_unset(MACAddr *macaddr);
  177. /**
  178. * qemu_find_nic_info: Obtain NIC configuration information
  179. * @typename: Name of device object type
  180. * @match_default: Match NIC configurations with no model specified
  181. * @alias: Additional model string to match (for user convenience and
  182. * backward compatibility).
  183. *
  184. * Search for a NIC configuration matching the NIC model constraints.
  185. */
  186. NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
  187. const char *alias);
  188. /**
  189. * qemu_configure_nic_device: Apply NIC configuration to a given device
  190. * @dev: Network device to be configured
  191. * @match_default: Match NIC configurations with no model specified
  192. * @alias: Additional model string to match
  193. *
  194. * Search for a NIC configuration for the provided device, using the
  195. * additionally specified matching constraints. If found, apply the
  196. * configuration using qdev_set_nic_properties() and return %true.
  197. *
  198. * This is used by platform code which creates the device anyway,
  199. * regardless of whether there is a configuration for it. This tends
  200. * to be platforms which ignore `--nodefaults` and create net devices
  201. * anyway, for example because the Ethernet device on that board is
  202. * always physically present.
  203. */
  204. bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
  205. const char *alias);
  206. /**
  207. * qemu_create_nic_device: Create a NIC device if a configuration exists for it
  208. * @typename: Object typename of network device
  209. * @match_default: Match NIC configurations with no model specified
  210. * @alias: Additional model string to match
  211. *
  212. * Search for a NIC configuration for the provided device type. If found,
  213. * create an object of the corresponding type and return it.
  214. */
  215. DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
  216. const char *alias);
  217. /*
  218. * qemu_create_nic_bus_devices: Create configured NIC devices for a given bus
  219. * @bus: Bus on which to create devices
  220. * @parent_type: Object type for devices to be created (e.g. TYPE_PCI_DEVICE)
  221. * @default_model: Object type name for default NIC model (or %NULL)
  222. * @alias: Additional model string to replace, for user convenience
  223. * @alias_target: Actual object type name to be used in place of @alias
  224. *
  225. * Instantiate dynamic NICs on a given bus, typically a PCI bus. This scans
  226. * for available NIC configurations which either specify a model which is
  227. * a child type of @parent_type, or which do not specify a model when
  228. * @default_model is non-NULL. Each device is instantiated on the given @bus.
  229. *
  230. * A single substitution is supported, e.g. "xen" → "xen-net-device" for the
  231. * Xen bus, or "virtio" → "virtio-net-pci" for PCI. This allows the user to
  232. * specify a more understandable "model=" parameter on the command line, not
  233. * only the real object typename.
  234. */
  235. void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type,
  236. const char *default_model,
  237. const char *alias, const char *alias_target);
  238. void print_net_client(Monitor *mon, NetClientState *nc);
  239. void net_socket_rs_init(SocketReadState *rs,
  240. SocketReadStateFinalize *finalize,
  241. bool vnet_hdr);
  242. NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
  243. /**
  244. * qemu_get_nic_models:
  245. * @device_type: Defines which devices should be taken into consideration
  246. * (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI)
  247. *
  248. * Get an array of pointers to names of NIC devices that are available in
  249. * the QEMU binary. The array is terminated with a NULL pointer entry.
  250. * The caller is responsible for freeing the memory when it is not required
  251. * anymore, e.g. with g_ptr_array_free(..., true).
  252. *
  253. * Returns: Pointer to the array that contains the pointers to the names.
  254. */
  255. GPtrArray *qemu_get_nic_models(const char *device_type);
  256. /* NIC info */
  257. #define MAX_NICS 8
  258. struct NICInfo {
  259. MACAddr macaddr;
  260. char *model;
  261. char *name;
  262. char *devaddr;
  263. NetClientState *netdev;
  264. int used; /* is this slot in nd_table[] being used? */
  265. int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
  266. int nvectors;
  267. };
  268. /* from net.c */
  269. extern NetClientStateList net_clients;
  270. bool netdev_is_modern(const char *optstr);
  271. void netdev_parse_modern(const char *optstr);
  272. void net_client_parse(QemuOptsList *opts_list, const char *optstr);
  273. void show_netdevs(void);
  274. void net_init_clients(void);
  275. void net_check_clients(void);
  276. void net_cleanup(void);
  277. void hmp_host_net_add(Monitor *mon, const QDict *qdict);
  278. void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
  279. void netdev_add(QemuOpts *opts, Error **errp);
  280. int net_hub_id_for_client(NetClientState *nc, int *id);
  281. #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup"
  282. #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown"
  283. #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
  284. #define DEFAULT_BRIDGE_INTERFACE "br0"
  285. void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
  286. #define POLYNOMIAL_BE 0x04c11db6
  287. #define POLYNOMIAL_LE 0xedb88320
  288. uint32_t net_crc32(const uint8_t *p, int len);
  289. uint32_t net_crc32_le(const uint8_t *p, int len);
  290. #define vmstate_offset_macaddr(_state, _field) \
  291. vmstate_offset_array(_state, _field.a, uint8_t, \
  292. sizeof(typeof_field(_state, _field)))
  293. #define VMSTATE_MACADDR(_field, _state) { \
  294. .name = (stringify(_field)), \
  295. .size = sizeof(MACAddr), \
  296. .info = &vmstate_info_buffer, \
  297. .flags = VMS_BUFFER, \
  298. .offset = vmstate_offset_macaddr(_state, _field), \
  299. }
  300. static inline bool net_peer_needs_padding(NetClientState *nc)
  301. {
  302. return nc->peer && !nc->peer->do_not_pad;
  303. }
  304. #endif