net.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #ifndef QEMU_NET_H
  2. #define QEMU_NET_H
  3. #include "qemu-queue.h"
  4. #include "qemu-common.h"
  5. #include "qdict.h"
  6. #include "qemu-option.h"
  7. #include "net/queue.h"
  8. #include "vmstate.h"
  9. struct MACAddr {
  10. uint8_t a[6];
  11. };
  12. /* qdev nic properties */
  13. typedef struct NICConf {
  14. MACAddr macaddr;
  15. VLANState *vlan;
  16. VLANClientState *peer;
  17. int32_t bootindex;
  18. } NICConf;
  19. #define DEFINE_NIC_PROPERTIES(_state, _conf) \
  20. DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \
  21. DEFINE_PROP_VLAN("vlan", _state, _conf.vlan), \
  22. DEFINE_PROP_NETDEV("netdev", _state, _conf.peer), \
  23. DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1)
  24. /* VLANs support */
  25. typedef enum {
  26. NET_CLIENT_TYPE_NONE,
  27. NET_CLIENT_TYPE_NIC,
  28. NET_CLIENT_TYPE_USER,
  29. NET_CLIENT_TYPE_TAP,
  30. NET_CLIENT_TYPE_SOCKET,
  31. NET_CLIENT_TYPE_VDE,
  32. NET_CLIENT_TYPE_DUMP,
  33. NET_CLIENT_TYPE_BRIDGE,
  34. NET_CLIENT_TYPE_MAX
  35. } net_client_type;
  36. typedef void (NetPoll)(VLANClientState *, bool enable);
  37. typedef int (NetCanReceive)(VLANClientState *);
  38. typedef ssize_t (NetReceive)(VLANClientState *, const uint8_t *, size_t);
  39. typedef ssize_t (NetReceiveIOV)(VLANClientState *, const struct iovec *, int);
  40. typedef void (NetCleanup) (VLANClientState *);
  41. typedef void (LinkStatusChanged)(VLANClientState *);
  42. typedef struct NetClientInfo {
  43. net_client_type type;
  44. size_t size;
  45. NetReceive *receive;
  46. NetReceive *receive_raw;
  47. NetReceiveIOV *receive_iov;
  48. NetCanReceive *can_receive;
  49. NetCleanup *cleanup;
  50. LinkStatusChanged *link_status_changed;
  51. NetPoll *poll;
  52. } NetClientInfo;
  53. struct VLANClientState {
  54. NetClientInfo *info;
  55. int link_down;
  56. QTAILQ_ENTRY(VLANClientState) next;
  57. struct VLANState *vlan;
  58. VLANClientState *peer;
  59. NetQueue *send_queue;
  60. char *model;
  61. char *name;
  62. char info_str[256];
  63. unsigned receive_disabled : 1;
  64. };
  65. typedef struct NICState {
  66. VLANClientState nc;
  67. NICConf *conf;
  68. void *opaque;
  69. bool peer_deleted;
  70. } NICState;
  71. struct VLANState {
  72. int id;
  73. QTAILQ_HEAD(, VLANClientState) clients;
  74. QTAILQ_ENTRY(VLANState) next;
  75. NetQueue *send_queue;
  76. };
  77. VLANState *qemu_find_vlan(int id, int allocate);
  78. VLANClientState *qemu_find_netdev(const char *id);
  79. VLANClientState *qemu_new_net_client(NetClientInfo *info,
  80. VLANState *vlan,
  81. VLANClientState *peer,
  82. const char *model,
  83. const char *name);
  84. NICState *qemu_new_nic(NetClientInfo *info,
  85. NICConf *conf,
  86. const char *model,
  87. const char *name,
  88. void *opaque);
  89. void qemu_del_vlan_client(VLANClientState *vc);
  90. VLANClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
  91. const char *client_str);
  92. typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
  93. void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
  94. int qemu_can_send_packet(VLANClientState *vc);
  95. ssize_t qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov,
  96. int iovcnt);
  97. ssize_t qemu_sendv_packet_async(VLANClientState *vc, const struct iovec *iov,
  98. int iovcnt, NetPacketSent *sent_cb);
  99. void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size);
  100. ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size);
  101. ssize_t qemu_send_packet_async(VLANClientState *vc, const uint8_t *buf,
  102. int size, NetPacketSent *sent_cb);
  103. void qemu_purge_queued_packets(VLANClientState *vc);
  104. void qemu_flush_queued_packets(VLANClientState *vc);
  105. void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]);
  106. void qemu_macaddr_default_if_unset(MACAddr *macaddr);
  107. int qemu_show_nic_models(const char *arg, const char *const *models);
  108. void qemu_check_nic_model(NICInfo *nd, const char *model);
  109. int qemu_find_nic_model(NICInfo *nd, const char * const *models,
  110. const char *default_model);
  111. void do_info_network(Monitor *mon);
  112. /* NIC info */
  113. #define MAX_NICS 8
  114. struct NICInfo {
  115. MACAddr macaddr;
  116. char *model;
  117. char *name;
  118. char *devaddr;
  119. VLANState *vlan;
  120. VLANClientState *netdev;
  121. int used; /* is this slot in nd_table[] being used? */
  122. int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
  123. int nvectors;
  124. };
  125. extern int nb_nics;
  126. extern NICInfo nd_table[MAX_NICS];
  127. extern int default_net;
  128. /* BT HCI info */
  129. struct HCIInfo {
  130. int (*bdaddr_set)(struct HCIInfo *hci, const uint8_t *bd_addr);
  131. void (*cmd_send)(struct HCIInfo *hci, const uint8_t *data, int len);
  132. void (*sco_send)(struct HCIInfo *hci, const uint8_t *data, int len);
  133. void (*acl_send)(struct HCIInfo *hci, const uint8_t *data, int len);
  134. void *opaque;
  135. void (*evt_recv)(void *opaque, const uint8_t *data, int len);
  136. void (*acl_recv)(void *opaque, const uint8_t *data, int len);
  137. };
  138. struct HCIInfo *qemu_next_hci(void);
  139. /* from net.c */
  140. extern const char *legacy_tftp_prefix;
  141. extern const char *legacy_bootp_filename;
  142. int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev);
  143. int net_client_parse(QemuOptsList *opts_list, const char *str);
  144. int net_init_clients(void);
  145. void net_check_clients(void);
  146. void net_cleanup(void);
  147. void net_host_device_add(Monitor *mon, const QDict *qdict);
  148. void net_host_device_remove(Monitor *mon, const QDict *qdict);
  149. int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
  150. int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
  151. #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
  152. #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
  153. #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
  154. #define DEFAULT_BRIDGE_INTERFACE "br0"
  155. void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
  156. int net_handle_fd_param(Monitor *mon, const char *param);
  157. #define POLYNOMIAL 0x04c11db6
  158. unsigned compute_mcast_idx(const uint8_t *ep);
  159. #define vmstate_offset_macaddr(_state, _field) \
  160. vmstate_offset_array(_state, _field.a, uint8_t, \
  161. sizeof(typeof_field(_state, _field)))
  162. #define VMSTATE_MACADDR(_field, _state) { \
  163. .name = (stringify(_field)), \
  164. .size = sizeof(MACAddr), \
  165. .info = &vmstate_info_buffer, \
  166. .flags = VMS_BUFFER, \
  167. .offset = vmstate_offset_macaddr(_state, _field), \
  168. }
  169. #endif