net.h 13 KB

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