79b21a0d55eb5143f0315b7de8c9381f6432d408
[cascardo/ovs.git] / lib / netdev.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef NETDEV_H
18 #define NETDEV_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include "openvswitch/types.h"
24 #include "packets.h"
25
26 #ifdef  __cplusplus
27 extern "C" {
28 #endif
29
30 /* Generic interface to network devices ("netdev"s).
31  *
32  * Every port on a switch must have a corresponding netdev that must minimally
33  * support a few operations, such as the ability to read the netdev's MTU.
34  * The PORTING file at the top of the source tree has more information in the
35  * "Writing a netdev Provider" section.
36  *
37  * Thread-safety
38  * =============
39  *
40  * Most of the netdev functions are fully thread-safe: they may be called from
41  * any number of threads on the same or different netdev objects.  The
42  * exceptions are:
43  *
44  *    netdev_rxq_recv()
45  *    netdev_rxq_wait()
46  *    netdev_rxq_drain()
47  *
48  *      These functions are conditionally thread-safe: they may be called from
49  *      different threads only on different netdev_rxq objects.  (The client may
50  *      create multiple netdev_rxq objects for a single netdev and access each
51  *      of those from a different thread.)
52  *
53  *    NETDEV_FOR_EACH_QUEUE
54  *    netdev_queue_dump_next()
55  *    netdev_queue_dump_done()
56  *
57  *      These functions are conditionally thread-safe: they may be called from
58  *      different threads only on different netdev_queue_dump objects.  (The
59  *      client may create multiple netdev_queue_dump objects for a single
60  *      netdev and access each of those from a different thread.)
61  */
62
63 struct dpif_packet;
64 struct netdev;
65 struct netdev_class;
66 struct netdev_rxq;
67 struct netdev_saved_flags;
68 struct ofpbuf;
69 struct in_addr;
70 struct in6_addr;
71 struct smap;
72 struct sset;
73
74 /* Network device statistics.
75  *
76  * Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
77 struct netdev_stats {
78     uint64_t rx_packets;        /* Total packets received. */
79     uint64_t tx_packets;        /* Total packets transmitted. */
80     uint64_t rx_bytes;          /* Total bytes received. */
81     uint64_t tx_bytes;          /* Total bytes transmitted. */
82     uint64_t rx_errors;         /* Bad packets received. */
83     uint64_t tx_errors;         /* Packet transmit problems. */
84     uint64_t rx_dropped;        /* No buffer space. */
85     uint64_t tx_dropped;        /* No buffer space. */
86     uint64_t multicast;         /* Multicast packets received. */
87     uint64_t collisions;
88
89     /* Detailed receive errors. */
90     uint64_t rx_length_errors;
91     uint64_t rx_over_errors;    /* Receiver ring buff overflow. */
92     uint64_t rx_crc_errors;     /* Recved pkt with crc error. */
93     uint64_t rx_frame_errors;   /* Recv'd frame alignment error. */
94     uint64_t rx_fifo_errors;    /* Recv'r fifo overrun . */
95     uint64_t rx_missed_errors;  /* Receiver missed packet. */
96
97     /* Detailed transmit errors. */
98     uint64_t tx_aborted_errors;
99     uint64_t tx_carrier_errors;
100     uint64_t tx_fifo_errors;
101     uint64_t tx_heartbeat_errors;
102     uint64_t tx_window_errors;
103 };
104
105 /* Configuration specific to tunnels. */
106 struct netdev_tunnel_config {
107     bool in_key_present;
108     bool in_key_flow;
109     ovs_be64 in_key;
110
111     bool out_key_present;
112     bool out_key_flow;
113     ovs_be64 out_key;
114
115     ovs_be16 dst_port;
116
117     bool ip_src_flow;
118     bool ip_dst_flow;
119     ovs_be32 ip_src;
120     ovs_be32 ip_dst;
121
122     uint8_t ttl;
123     bool ttl_inherit;
124
125     uint8_t tos;
126     bool tos_inherit;
127
128     bool csum;
129     bool ipsec;
130     bool dont_fragment;
131 };
132
133 void netdev_run(void);
134 void netdev_wait(void);
135
136 void netdev_enumerate_types(struct sset *types);
137 bool netdev_is_reserved_name(const char *name);
138
139 int netdev_n_txq(const struct netdev *netdev);
140 int netdev_n_rxq(const struct netdev *netdev);
141 bool netdev_is_pmd(const struct netdev *netdev);
142
143 /* Open and close. */
144 int netdev_open(const char *name, const char *type, struct netdev **netdevp);
145
146 struct netdev *netdev_ref(const struct netdev *);
147 void netdev_remove(struct netdev *);
148 void netdev_close(struct netdev *);
149
150 void netdev_parse_name(const char *netdev_name, char **name, char **type);
151
152 /* Options. */
153 int netdev_set_config(struct netdev *, const struct smap *args, char **errp);
154 int netdev_get_config(const struct netdev *, struct smap *);
155 const struct netdev_tunnel_config *
156     netdev_get_tunnel_config(const struct netdev *);
157 int netdev_get_numa_id(const struct netdev *);
158
159 /* Basic properties. */
160 const char *netdev_get_name(const struct netdev *);
161 const char *netdev_get_type(const struct netdev *);
162 const char *netdev_get_type_from_name(const char *);
163 int netdev_get_mtu(const struct netdev *, int *mtup);
164 int netdev_set_mtu(const struct netdev *, int mtu);
165 int netdev_get_ifindex(const struct netdev *);
166 int netdev_set_multiq(struct netdev *, unsigned int n_txq, unsigned int n_rxq);
167
168 /* Packet reception. */
169 int netdev_rxq_open(struct netdev *, struct netdev_rxq **, int id);
170 void netdev_rxq_close(struct netdev_rxq *);
171
172 const char *netdev_rxq_get_name(const struct netdev_rxq *);
173
174 int netdev_rxq_recv(struct netdev_rxq *rx, struct dpif_packet **buffers,
175                     int *cnt);
176 void netdev_rxq_wait(struct netdev_rxq *);
177 int netdev_rxq_drain(struct netdev_rxq *);
178
179 /* Packet transmission. */
180 int netdev_send(struct netdev *, int qid, struct dpif_packet **, int cnt,
181                 bool may_steal);
182 void netdev_send_wait(struct netdev *, int qid);
183
184 /* Hardware address. */
185 int netdev_set_etheraddr(struct netdev *, const uint8_t mac[ETH_ADDR_LEN]);
186 int netdev_get_etheraddr(const struct netdev *, uint8_t mac[ETH_ADDR_LEN]);
187
188 /* PHY interface. */
189 bool netdev_get_carrier(const struct netdev *);
190 long long int netdev_get_carrier_resets(const struct netdev *);
191 int netdev_set_miimon_interval(struct netdev *, long long int interval);
192
193 /* Features. */
194 enum netdev_features {
195     NETDEV_F_10MB_HD =    1 << 0,  /* 10 Mb half-duplex rate support. */
196     NETDEV_F_10MB_FD =    1 << 1,  /* 10 Mb full-duplex rate support. */
197     NETDEV_F_100MB_HD =   1 << 2,  /* 100 Mb half-duplex rate support. */
198     NETDEV_F_100MB_FD =   1 << 3,  /* 100 Mb full-duplex rate support. */
199     NETDEV_F_1GB_HD =     1 << 4,  /* 1 Gb half-duplex rate support. */
200     NETDEV_F_1GB_FD =     1 << 5,  /* 1 Gb full-duplex rate support. */
201     NETDEV_F_10GB_FD =    1 << 6,  /* 10 Gb full-duplex rate support. */
202     NETDEV_F_40GB_FD =    1 << 7,  /* 40 Gb full-duplex rate support. */
203     NETDEV_F_100GB_FD =   1 << 8,  /* 100 Gb full-duplex rate support. */
204     NETDEV_F_1TB_FD =     1 << 9,  /* 1 Tb full-duplex rate support. */
205     NETDEV_F_OTHER =      1 << 10, /* Other rate, not in the list. */
206     NETDEV_F_COPPER =     1 << 11, /* Copper medium. */
207     NETDEV_F_FIBER =      1 << 12, /* Fiber medium. */
208     NETDEV_F_AUTONEG =    1 << 13, /* Auto-negotiation. */
209     NETDEV_F_PAUSE =      1 << 14, /* Pause. */
210     NETDEV_F_PAUSE_ASYM = 1 << 15, /* Asymmetric pause. */
211 };
212
213 int netdev_get_features(const struct netdev *,
214                         enum netdev_features *current,
215                         enum netdev_features *advertised,
216                         enum netdev_features *supported,
217                         enum netdev_features *peer);
218 uint64_t netdev_features_to_bps(enum netdev_features features,
219                                 uint64_t default_bps);
220 bool netdev_features_is_full_duplex(enum netdev_features features);
221 int netdev_set_advertisements(struct netdev *, enum netdev_features advertise);
222
223 /* Flags. */
224 enum netdev_flags {
225     NETDEV_UP = 0x0001,         /* Device enabled? */
226     NETDEV_PROMISC = 0x0002,    /* Promiscuous mode? */
227     NETDEV_LOOPBACK = 0x0004    /* This is a loopback device. */
228 };
229
230 int netdev_get_flags(const struct netdev *, enum netdev_flags *);
231 int netdev_set_flags(struct netdev *, enum netdev_flags,
232                      struct netdev_saved_flags **);
233 int netdev_turn_flags_on(struct netdev *, enum netdev_flags,
234                          struct netdev_saved_flags **);
235 int netdev_turn_flags_off(struct netdev *, enum netdev_flags,
236                           struct netdev_saved_flags **);
237
238 void netdev_restore_flags(struct netdev_saved_flags *);
239
240 /* TCP/IP stack interface. */
241 int netdev_get_in4(const struct netdev *, struct in_addr *address,
242                    struct in_addr *netmask);
243 int netdev_set_in4(struct netdev *, struct in_addr addr, struct in_addr mask);
244 int netdev_get_in4_by_name(const char *device_name, struct in_addr *in4);
245 int netdev_get_in6(const struct netdev *, struct in6_addr *);
246 int netdev_add_router(struct netdev *, struct in_addr router);
247 int netdev_get_next_hop(const struct netdev *, const struct in_addr *host,
248                         struct in_addr *next_hop, char **);
249 int netdev_get_status(const struct netdev *, struct smap *);
250 int netdev_arp_lookup(const struct netdev *, ovs_be32 ip,
251                       uint8_t mac[ETH_ADDR_LEN]);
252
253 struct netdev *netdev_find_dev_by_in4(const struct in_addr *);
254
255 /* Statistics. */
256 int netdev_get_stats(const struct netdev *, struct netdev_stats *);
257
258 /* Quality of service. */
259 struct netdev_qos_capabilities {
260     unsigned int n_queues;
261 };
262
263 struct netdev_queue_stats {
264     /* Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
265     uint64_t tx_bytes;
266     uint64_t tx_packets;
267     uint64_t tx_errors;
268
269     /* Time at which the queue was created, in msecs, LLONG_MIN if unknown. */
270     long long int created;
271 };
272
273 int netdev_set_policing(struct netdev *, uint32_t kbits_rate,
274                         uint32_t kbits_burst);
275
276 int netdev_get_qos_types(const struct netdev *, struct sset *types);
277 int netdev_get_qos_capabilities(const struct netdev *,
278                                 const char *type,
279                                 struct netdev_qos_capabilities *);
280 int netdev_get_n_queues(const struct netdev *,
281                         const char *type, unsigned int *n_queuesp);
282
283 int netdev_get_qos(const struct netdev *,
284                    const char **typep, struct smap *details);
285 int netdev_set_qos(struct netdev *,
286                    const char *type, const struct smap *details);
287
288 int netdev_get_queue(const struct netdev *,
289                      unsigned int queue_id, struct smap *details);
290 int netdev_set_queue(struct netdev *,
291                      unsigned int queue_id, const struct smap *details);
292 int netdev_delete_queue(struct netdev *, unsigned int queue_id);
293 int netdev_get_queue_stats(const struct netdev *, unsigned int queue_id,
294                            struct netdev_queue_stats *);
295 uint64_t netdev_get_change_seq(const struct netdev *);
296
297 struct netdev_queue_dump {
298     struct netdev *netdev;
299     int error;
300     void *state;
301 };
302 void netdev_queue_dump_start(struct netdev_queue_dump *,
303                              const struct netdev *);
304 bool netdev_queue_dump_next(struct netdev_queue_dump *,
305                             unsigned int *queue_id, struct smap *details);
306 int netdev_queue_dump_done(struct netdev_queue_dump *);
307
308 /* Iterates through each queue in NETDEV, using DUMP as state.  Fills QUEUE_ID
309  * and DETAILS with information about queues.  The client must initialize and
310  * destroy DETAILS.
311  *
312  * Arguments all have pointer type.
313  *
314  * If you break out of the loop, then you need to free the dump structure by
315  * hand using netdev_queue_dump_done(). */
316 #define NETDEV_QUEUE_FOR_EACH(QUEUE_ID, DETAILS, DUMP, NETDEV)  \
317     for (netdev_queue_dump_start(DUMP, NETDEV);                 \
318          (netdev_queue_dump_next(DUMP, QUEUE_ID, DETAILS)       \
319           ? true                                                \
320           : (netdev_queue_dump_done(DUMP), false));             \
321         )
322
323 typedef void netdev_dump_queue_stats_cb(unsigned int queue_id,
324                                         struct netdev_queue_stats *,
325                                         void *aux);
326 int netdev_dump_queue_stats(const struct netdev *,
327                             netdev_dump_queue_stats_cb *, void *aux);
328
329 enum { NETDEV_MAX_RX_BATCH = 256 };     /* Maximum number packets in rx_recv() batch. */
330
331 #ifdef  __cplusplus
332 }
333 #endif
334
335 #endif /* netdev.h */