qemu/include/net/net.h
<<
>>
Prefs
   1#ifndef QEMU_NET_H
   2#define QEMU_NET_H
   3
   4#include "qemu/queue.h"
   5#include "qemu-common.h"
   6#include "qapi/qmp/qdict.h"
   7#include "qemu/option.h"
   8#include "net/queue.h"
   9#include "migration/vmstate.h"
  10#include "qapi-types.h"
  11
  12#define MAX_QUEUE_NUM 1024
  13
  14struct MACAddr {
  15    uint8_t a[6];
  16};
  17
  18/* qdev nic properties */
  19
  20typedef struct NICPeers {
  21    NetClientState *ncs[MAX_QUEUE_NUM];
  22} NICPeers;
  23
  24typedef struct NICConf {
  25    MACAddr macaddr;
  26    NICPeers peers;
  27    int32_t bootindex;
  28    int32_t queues;
  29} NICConf;
  30
  31#define DEFINE_NIC_PROPERTIES(_state, _conf)                            \
  32    DEFINE_PROP_MACADDR("mac",   _state, _conf.macaddr),                \
  33    DEFINE_PROP_VLAN("vlan",     _state, _conf.peers),                   \
  34    DEFINE_PROP_NETDEV("netdev", _state, _conf.peers),                   \
  35    DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1)
  36
  37
  38/* Net clients */
  39
  40typedef void (NetPoll)(NetClientState *, bool enable);
  41typedef int (NetCanReceive)(NetClientState *);
  42typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
  43typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
  44typedef void (NetCleanup) (NetClientState *);
  45typedef void (LinkStatusChanged)(NetClientState *);
  46typedef void (NetClientDestructor)(NetClientState *);
  47
  48typedef struct NetClientInfo {
  49    NetClientOptionsKind type;
  50    size_t size;
  51    NetReceive *receive;
  52    NetReceive *receive_raw;
  53    NetReceiveIOV *receive_iov;
  54    NetCanReceive *can_receive;
  55    NetCleanup *cleanup;
  56    LinkStatusChanged *link_status_changed;
  57    NetPoll *poll;
  58} NetClientInfo;
  59
  60struct NetClientState {
  61    NetClientInfo *info;
  62    int link_down;
  63    QTAILQ_ENTRY(NetClientState) next;
  64    NetClientState *peer;
  65    NetQueue *send_queue;
  66    char *model;
  67    char *name;
  68    char info_str[256];
  69    unsigned receive_disabled : 1;
  70    NetClientDestructor *destructor;
  71    unsigned int queue_index;
  72};
  73
  74typedef struct NICState {
  75    NetClientState *ncs;
  76    NICConf *conf;
  77    void *opaque;
  78    bool peer_deleted;
  79} NICState;
  80
  81NetClientState *qemu_find_netdev(const char *id);
  82int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
  83                                 NetClientOptionsKind type, int max);
  84NetClientState *qemu_new_net_client(NetClientInfo *info,
  85                                    NetClientState *peer,
  86                                    const char *model,
  87                                    const char *name);
  88NICState *qemu_new_nic(NetClientInfo *info,
  89                       NICConf *conf,
  90                       const char *model,
  91                       const char *name,
  92                       void *opaque);
  93void qemu_del_nic(NICState *nic);
  94NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
  95NetClientState *qemu_get_queue(NICState *nic);
  96NICState *qemu_get_nic(NetClientState *nc);
  97void *qemu_get_nic_opaque(NetClientState *nc);
  98void qemu_del_net_client(NetClientState *nc);
  99NetClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
 100                                              const char *client_str);
 101typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
 102void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
 103int qemu_can_send_packet(NetClientState *nc);
 104ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
 105                          int iovcnt);
 106ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
 107                                int iovcnt, NetPacketSent *sent_cb);
 108void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
 109ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
 110ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
 111                               int size, NetPacketSent *sent_cb);
 112void qemu_purge_queued_packets(NetClientState *nc);
 113void qemu_flush_queued_packets(NetClientState *nc);
 114void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
 115void qemu_macaddr_default_if_unset(MACAddr *macaddr);
 116int qemu_show_nic_models(const char *arg, const char *const *models);
 117void qemu_check_nic_model(NICInfo *nd, const char *model);
 118int qemu_find_nic_model(NICInfo *nd, const char * const *models,
 119                        const char *default_model);
 120
 121ssize_t qemu_deliver_packet(NetClientState *sender,
 122                            unsigned flags,
 123                            const uint8_t *data,
 124                            size_t size,
 125                            void *opaque);
 126ssize_t qemu_deliver_packet_iov(NetClientState *sender,
 127                            unsigned flags,
 128                            const struct iovec *iov,
 129                            int iovcnt,
 130                            void *opaque);
 131
 132void print_net_client(Monitor *mon, NetClientState *nc);
 133void do_info_network(Monitor *mon, const QDict *qdict);
 134
 135/* NIC info */
 136
 137#define MAX_NICS 8
 138
 139struct NICInfo {
 140    MACAddr macaddr;
 141    char *model;
 142    char *name;
 143    char *devaddr;
 144    NetClientState *netdev;
 145    int used;         /* is this slot in nd_table[] being used? */
 146    int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
 147    int nvectors;
 148};
 149
 150extern int nb_nics;
 151extern NICInfo nd_table[MAX_NICS];
 152extern int default_net;
 153
 154/* from net.c */
 155extern const char *legacy_tftp_prefix;
 156extern const char *legacy_bootp_filename;
 157
 158int net_client_init(QemuOpts *opts, int is_netdev, Error **errp);
 159int net_client_parse(QemuOptsList *opts_list, const char *str);
 160int net_init_clients(void);
 161void net_check_clients(void);
 162void net_cleanup(void);
 163void net_host_device_add(Monitor *mon, const QDict *qdict);
 164void net_host_device_remove(Monitor *mon, const QDict *qdict);
 165void netdev_add(QemuOpts *opts, Error **errp);
 166int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret);
 167
 168int net_hub_id_for_client(NetClientState *nc, int *id);
 169NetClientState *net_hub_port_find(int hub_id);
 170
 171#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
 172#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
 173#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
 174#define DEFAULT_BRIDGE_INTERFACE "br0"
 175
 176void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
 177
 178#define POLYNOMIAL 0x04c11db6
 179unsigned compute_mcast_idx(const uint8_t *ep);
 180
 181#define vmstate_offset_macaddr(_state, _field)                       \
 182    vmstate_offset_array(_state, _field.a, uint8_t,                \
 183                         sizeof(typeof_field(_state, _field)))
 184
 185#define VMSTATE_MACADDR(_field, _state) {                            \
 186    .name       = (stringify(_field)),                               \
 187    .size       = sizeof(MACAddr),                                   \
 188    .info       = &vmstate_info_buffer,                              \
 189    .flags      = VMS_BUFFER,                                        \
 190    .offset     = vmstate_offset_macaddr(_state, _field),            \
 191}
 192
 193#endif
 194