2
0

net.h 5.9 KB

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