~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/netdev-provider.h

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
/* A network device (e.g. an Ethernet device).
32
32
 *
33
 
 * This structure should be treated as opaque by network device
34
 
 * implementations. */
35
 
struct netdev_dev {
 
33
 * Network device implementations should treat this structure as opaque. */
 
34
struct netdev {
36
35
    char *name;                         /* Name of network device. */
37
36
    const struct netdev_class *netdev_class; /* Functions to control
38
37
                                                this device. */
39
38
    int ref_cnt;                        /* Times this devices was opened. */
40
39
    struct shash_node *node;            /* Pointer to element in global map. */
 
40
    struct list saved_flags_list; /* Contains "struct netdev_saved_flags". */
41
41
};
42
42
 
43
 
void netdev_dev_init(struct netdev_dev *, const char *name,
44
 
                     const struct netdev_class *);
45
 
void netdev_dev_uninit(struct netdev_dev *, bool destroy);
46
 
const char *netdev_dev_get_type(const struct netdev_dev *);
47
 
const struct netdev_class *netdev_dev_get_class(const struct netdev_dev *);
48
 
const char *netdev_dev_get_name(const struct netdev_dev *);
49
 
struct netdev_dev *netdev_dev_from_name(const char *name);
50
 
void netdev_dev_get_devices(const struct netdev_class *,
51
 
                            struct shash *device_list);
 
43
void netdev_init(struct netdev *, const char *name,
 
44
                 const struct netdev_class *);
 
45
void netdev_uninit(struct netdev *, bool destroy);
 
46
const char *netdev_get_type(const struct netdev *);
 
47
const struct netdev_class *netdev_get_class(const struct netdev *);
 
48
const char *netdev_get_name(const struct netdev *);
 
49
struct netdev *netdev_from_name(const char *name);
 
50
void netdev_get_devices(const struct netdev_class *,
 
51
                        struct shash *device_list);
52
52
 
53
 
static inline void netdev_dev_assert_class(const struct netdev_dev *netdev_dev,
 
53
static inline void netdev_assert_class(const struct netdev *netdev,
54
54
                                           const struct netdev_class *class_)
55
55
{
56
 
    ovs_assert(netdev_dev->netdev_class == class_);
57
 
}
58
 
 
59
 
/* A instance of an open network device.
60
 
 *
61
 
 * This structure should be treated as opaque by network device
62
 
 * implementations. */
63
 
struct netdev {
64
 
    struct netdev_dev *netdev_dev;   /* Parent netdev_dev. */
65
 
    struct list node;                /* Element in global list. */
66
 
 
67
 
    enum netdev_flags save_flags;    /* Initial device flags. */
68
 
    enum netdev_flags changed_flags; /* Flags that we changed. */
69
 
};
70
 
 
71
 
void netdev_init(struct netdev *, struct netdev_dev *);
72
 
void netdev_uninit(struct netdev *, bool close);
73
 
struct netdev_dev *netdev_get_dev(const struct netdev *);
74
 
 
75
 
static inline void netdev_assert_class(const struct netdev *netdev,
76
 
                                       const struct netdev_class *netdev_class)
77
 
{
78
 
    netdev_dev_assert_class(netdev_get_dev(netdev), netdev_class);
 
56
    ovs_assert(netdev->netdev_class == class_);
79
57
}
80
58
 
81
59
/* Network device class structure, to be defined by each implementation of a
112
90
    void (*wait)(void);
113
91
 
114
92
    /* Attempts to create a network device named 'name' in 'netdev_class'.  On
115
 
     * success sets 'netdev_devp' to the newly created device. */
 
93
     * success sets 'netdevp' to the newly created device. */
116
94
    int (*create)(const struct netdev_class *netdev_class, const char *name,
117
 
                  struct netdev_dev **netdev_devp);
 
95
                  struct netdev **netdevp);
118
96
 
119
 
    /* Destroys 'netdev_dev'.
 
97
    /* Destroys 'netdev'.
120
98
     *
121
99
     * Netdev devices maintain a reference count that is incremented on
122
 
     * netdev_open() and decremented on netdev_close().  If 'netdev_dev'
 
100
     * netdev_open() and decremented on netdev_close().  If 'netdev'
123
101
     * has a non-zero reference count, then this function will not be
124
102
     * called. */
125
 
    void (*destroy)(struct netdev_dev *netdev_dev);
 
103
    void (*destroy)(struct netdev *netdev);
126
104
 
127
 
    /* Fetches the device 'netdev_dev''s configuration, storing it in 'args'.
 
105
    /* Fetches the device 'netdev''s configuration, storing it in 'args'.
128
106
     * The caller owns 'args' and pre-initializes it to an empty smap.
129
107
     *
130
108
     * If this netdev class does not have any configuration options, this may
131
109
     * be a null pointer. */
132
 
    int (*get_config)(struct netdev_dev *netdev_dev, struct smap *args);
 
110
    int (*get_config)(const struct netdev *netdev, struct smap *args);
133
111
 
134
 
    /* Changes the device 'netdev_dev''s configuration to 'args'.
 
112
    /* Changes the device 'netdev''s configuration to 'args'.
135
113
     *
136
114
     * If this netdev class does not support configuration, this may be a null
137
115
     * pointer. */
138
 
    int (*set_config)(struct netdev_dev *netdev_dev, const struct smap *args);
 
116
    int (*set_config)(struct netdev *netdev, const struct smap *args);
139
117
 
140
 
    /* Returns the tunnel configuration of 'netdev_dev'.  If 'netdev_dev' is
 
118
    /* Returns the tunnel configuration of 'netdev'.  If 'netdev' is
141
119
     * not a tunnel, returns null.
142
120
     *
143
121
     * If this function would always return null, it may be null instead. */
144
122
    const struct netdev_tunnel_config *
145
 
        (*get_tunnel_config)(const struct netdev_dev *netdev_dev);
146
 
 
147
 
    /* Attempts to open a network device.  On success, sets 'netdevp'
148
 
     * to the new network device. */
149
 
    int (*open)(struct netdev_dev *netdev_dev, struct netdev **netdevp);
150
 
 
151
 
    /* Closes 'netdev'. */
152
 
    void (*close)(struct netdev *netdev);
153
 
 
154
 
/* ## ----------------- ## */
155
 
/* ## Receiving Packets ## */
156
 
/* ## ----------------- ## */
157
 
 
158
 
/* The network provider interface is mostly used for inspecting and configuring
159
 
 * device "metadata", not for sending and receiving packets directly.  It may
160
 
 * be impractical to implement these functions on some operating systems and
161
 
 * hardware.  These functions may all be NULL in such cases.
162
 
 *
163
 
 * (However, the "dpif-netdev" implementation, which is the easiest way to
164
 
 * integrate Open vSwitch with a new operating system or hardware, does require
165
 
 * the ability to receive packets.) */
166
 
 
167
 
    /* Attempts to set up 'netdev' for receiving packets with ->recv().
168
 
     * Returns 0 if successful, otherwise a positive errno value.  Return
 
123
        (*get_tunnel_config)(const struct netdev *netdev);
 
124
 
 
125
    /* Attempts to open a netdev_rx for receiving packets from 'netdev'.
 
126
     * Returns 0 if successful, otherwise a positive errno value.  Returns
169
127
     * EOPNOTSUPP to indicate that the network device does not implement packet
170
128
     * reception through this interface.  This function may be set to null if
171
129
     * it would always return EOPNOTSUPP anyhow.  (This will prevent the
172
130
     * network device from being usefully used by the netdev-based "userspace
173
 
     * datapath".)*/
174
 
    int (*listen)(struct netdev *netdev);
175
 
 
176
 
    /* Attempts to receive a packet from 'netdev' into the 'size' bytes in
177
 
     * 'buffer'.  If successful, returns the number of bytes in the received
178
 
     * packet, otherwise a negative errno value.  Returns -EAGAIN immediately
179
 
     * if no packet is ready to be received.
180
 
     *
181
 
     * Returns -EMSGSIZE, and discards the packet, if the received packet is
182
 
     * longer than 'size' bytes.
183
 
     *
184
 
     * This function can only be expected to return a packet if ->listen() has
185
 
     * been called successfully.
186
 
     *
187
 
     * May be null if not needed, such as for a network device that does not
188
 
     * implement packet reception through the 'recv' member function. */
189
 
    int (*recv)(struct netdev *netdev, void *buffer, size_t size);
190
 
 
191
 
    /* Registers with the poll loop to wake up from the next call to
192
 
     * poll_block() when a packet is ready to be received with netdev_recv() on
193
 
     * 'netdev'.
194
 
     *
195
 
     * May be null if not needed, such as for a network device that does not
196
 
     * implement packet reception through the 'recv' member function. */
197
 
    void (*recv_wait)(struct netdev *netdev);
198
 
 
199
 
    /* Discards all packets waiting to be received from 'netdev'.
200
 
     *
201
 
     * May be null if not needed, such as for a network device that does not
202
 
     * implement packet reception through the 'recv' member function. */
203
 
    int (*drain)(struct netdev *netdev);
204
 
 
 
131
     * datapath".)
 
132
     *
 
133
     * On success, the implementation must set '*rxp' to a 'netdev_rx' for
 
134
     * 'netdev' that it has already initialized (with netdev_rx_init()). */
 
135
    int (*rx_open)(struct netdev *netdev, struct netdev_rx **rxp);
 
136
 
205
137
    /* Sends the 'size'-byte packet in 'buffer' on 'netdev'.  Returns 0 if
206
138
     * successful, otherwise a positive errno value.  Returns EAGAIN without
207
139
     * blocking if the packet cannot be queued immediately.  Returns EMSGSIZE
538
470
     * anyhow. */
539
471
    int (*add_router)(struct netdev *netdev, struct in_addr router);
540
472
 
541
 
    /* Looks up the next hop for 'host'.  If succesful, stores the next hop
 
473
    /* Looks up the next hop for 'host'.  If successful, stores the next hop
542
474
     * gateway's address (0 if 'host' is on a directly connected network) in
543
475
     * '*next_hop' and a copy of the name of the device to reach 'host' in
544
476
     * '*netdev_name', and returns 0.  The caller is responsible for freeing
572
504
    int (*arp_lookup)(const struct netdev *netdev, ovs_be32 ip,
573
505
                      uint8_t mac[6]);
574
506
 
575
 
    /* Retrieves the current set of flags on 'netdev' into '*old_flags'.
576
 
     * Then, turns off the flags that are set to 1 in 'off' and turns on the
577
 
     * flags that are set to 1 in 'on'.  (No bit will be set to 1 in both 'off'
578
 
     * and 'on'; that is, off & on == 0.)
 
507
    /* Retrieves the current set of flags on 'netdev' into '*old_flags'.  Then,
 
508
     * turns off the flags that are set to 1 in 'off' and turns on the flags
 
509
     * that are set to 1 in 'on'.  (No bit will be set to 1 in both 'off' and
 
510
     * 'on'; that is, off & on == 0.)
579
511
     *
580
512
     * This function may be invoked from a signal handler.  Therefore, it
581
513
     * should not do anything that is not signal-safe (such as logging). */
593
525
     * change, although implementations should try to avoid this. */
594
526
    unsigned int (*change_seq)(const struct netdev *netdev);
595
527
};
 
528
 
 
529
/* A data structure for capturing packets received by a network device.
 
530
 *
 
531
 * This structure should be treated as opaque by network device
 
532
 * implementations. */
 
533
struct netdev_rx {
 
534
    const struct netdev_rx_class *rx_class;
 
535
    struct netdev *netdev;
 
536
};
 
537
 
 
538
void netdev_rx_init(struct netdev_rx *, struct netdev *,
 
539
                    const struct netdev_rx_class *);
 
540
void netdev_rx_uninit(struct netdev_rx *);
 
541
struct netdev *netdev_rx_get_netdev(const struct netdev_rx *);
 
542
 
 
543
struct netdev_rx_class {
 
544
    /* Destroys 'rx'. */
 
545
    void (*destroy)(struct netdev_rx *rx);
 
546
 
 
547
    /* Attempts to receive a packet from 'rx' into the 'size' bytes in
 
548
     * 'buffer'.  If successful, returns the number of bytes in the received
 
549
     * packet, otherwise a negative errno value.  Returns -EAGAIN immediately
 
550
     * if no packet is ready to be received.
 
551
     *
 
552
     * Must return -EMSGSIZE, and discard the packet, if the received packet
 
553
     * is longer than 'size' bytes. */
 
554
    int (*recv)(struct netdev_rx *rx, void *buffer, size_t size);
 
555
 
 
556
    /* Registers with the poll loop to wake up from the next call to
 
557
     * poll_block() when a packet is ready to be received with netdev_rx_recv()
 
558
     * on 'rx'. */
 
559
    void (*wait)(struct netdev_rx *rx);
 
560
 
 
561
    /* Discards all packets waiting to be received from 'rx'. */
 
562
    int (*drain)(struct netdev_rx *rx);
 
563
};
 
564
 
 
565
static inline void netdev_rx_assert_class(const struct netdev_rx *rx,
 
566
                                          const struct netdev_rx_class *class_)
 
567
{
 
568
    ovs_assert(rx->rx_class == class_);
 
569
}
596
570
 
597
571
int netdev_register_provider(const struct netdev_class *);
598
572
int netdev_unregister_provider(const char *type);
601
575
extern const struct netdev_class netdev_linux_class;
602
576
extern const struct netdev_class netdev_internal_class;
603
577
extern const struct netdev_class netdev_tap_class;
604
 
#ifdef __FreeBSD__
 
578
#if defined(__FreeBSD__) || defined(__NetBSD__)
605
579
extern const struct netdev_class netdev_bsd_class;
606
580
#endif
607
581