1 /******************************************************************************
2  * netif.h
3  *
4  * Unified network-device I/O interface for Xen guest OSes.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Copyright (c) 2003-2004, Keir Fraser
25  */
26 
27 #ifndef __XEN_PUBLIC_IO_NETIF_H__
28 #define __XEN_PUBLIC_IO_NETIF_H__
29 
30 #include "ring.h"
31 #include "../grant_table.h"
32 
33 /*
34  * Older implementation of Xen network frontend / backend has an
35  * implicit dependency on the MAX_SKB_FRAGS as the maximum number of
36  * ring slots a skb can use. Netfront / netback may not work as
37  * expected when frontend and backend have different MAX_SKB_FRAGS.
38  *
39  * A better approach is to add mechanism for netfront / netback to
40  * negotiate this value. However we cannot fix all possible
41  * frontends, so we need to define a value which states the minimum
42  * slots backend must support.
43  *
44  * The minimum value derives from older Linux kernel's MAX_SKB_FRAGS
45  * (18), which is proved to work with most frontends. Any new backend
46  * which doesn't negotiate with frontend should expect frontend to
47  * send a valid packet using slots up to this value.
48  */
49 #define XEN_NETIF_NR_SLOTS_MIN 18
50 
51 /*
52  * Notifications after enqueuing any type of message should be conditional on
53  * the appropriate req_event or rsp_event field in the shared ring.
54  * If the client sends notification for rx requests then it should specify
55  * feature 'feature-rx-notify' via xenbus. Otherwise the backend will assume
56  * that it cannot safely queue packets (as it may not be kicked to send them).
57  */
58 
59 /*
60  * "feature-split-event-channels" is introduced to separate guest TX
61  * and RX notification. Backend either doesn't support this feature or
62  * advertises it via xenstore as 0 (disabled) or 1 (enabled).
63  *
64  * To make use of this feature, frontend should allocate two event
65  * channels for TX and RX, advertise them to backend as
66  * "event-channel-tx" and "event-channel-rx" respectively. If frontend
67  * doesn't want to use this feature, it just writes "event-channel"
68  * node as before.
69  */
70 
71 /*
72  * Multiple transmit and receive queues:
73  * If supported, the backend will write the key "multi-queue-max-queues" to
74  * the directory for that vif, and set its value to the maximum supported
75  * number of queues.
76  * Frontends that are aware of this feature and wish to use it can write the
77  * key "multi-queue-num-queues", set to the number they wish to use, which
78  * must be greater than zero, and no more than the value reported by the backend
79  * in "multi-queue-max-queues".
80  *
81  * Queues replicate the shared rings and event channels.
82  * "feature-split-event-channels" may optionally be used when using
83  * multiple queues, but is not mandatory.
84  *
85  * Each queue consists of one shared ring pair, i.e. there must be the same
86  * number of tx and rx rings.
87  *
88  * For frontends requesting just one queue, the usual event-channel and
89  * ring-ref keys are written as before, simplifying the backend processing
90  * to avoid distinguishing between a frontend that doesn't understand the
91  * multi-queue feature, and one that does, but requested only one queue.
92  *
93  * Frontends requesting two or more queues must not write the toplevel
94  * event-channel (or event-channel-{tx,rx}) and {tx,rx}-ring-ref keys,
95  * instead writing those keys under sub-keys having the name "queue-N" where
96  * N is the integer ID of the queue for which those keys belong. Queues
97  * are indexed from zero. For example, a frontend with two queues and split
98  * event channels must write the following set of queue-related keys:
99  *
100  * /local/domain/1/device/vif/0/multi-queue-num-queues = "2"
101  * /local/domain/1/device/vif/0/queue-0 = ""
102  * /local/domain/1/device/vif/0/queue-0/tx-ring-ref = "<ring-ref-tx0>"
103  * /local/domain/1/device/vif/0/queue-0/rx-ring-ref = "<ring-ref-rx0>"
104  * /local/domain/1/device/vif/0/queue-0/event-channel-tx = "<evtchn-tx0>"
105  * /local/domain/1/device/vif/0/queue-0/event-channel-rx = "<evtchn-rx0>"
106  * /local/domain/1/device/vif/0/queue-1 = ""
107  * /local/domain/1/device/vif/0/queue-1/tx-ring-ref = "<ring-ref-tx1>"
108  * /local/domain/1/device/vif/0/queue-1/rx-ring-ref = "<ring-ref-rx1"
109  * /local/domain/1/device/vif/0/queue-1/event-channel-tx = "<evtchn-tx1>"
110  * /local/domain/1/device/vif/0/queue-1/event-channel-rx = "<evtchn-rx1>"
111  *
112  * If there is any inconsistency in the XenStore data, the backend may
113  * choose not to connect any queues, instead treating the request as an
114  * error. This includes scenarios where more (or fewer) queues were
115  * requested than the frontend provided details for.
116  *
117  * Mapping of packets to queues is considered to be a function of the
118  * transmitting system (backend or frontend) and is not negotiated
119  * between the two. Guests are free to transmit packets on any queue
120  * they choose, provided it has been set up correctly. Guests must be
121  * prepared to receive packets on any queue they have requested be set up.
122  */
123 
124 /*
125  * "feature-no-csum-offload" should be used to turn IPv4 TCP/UDP checksum
126  * offload off or on. If it is missing then the feature is assumed to be on.
127  * "feature-ipv6-csum-offload" should be used to turn IPv6 TCP/UDP checksum
128  * offload on or off. If it is missing then the feature is assumed to be off.
129  */
130 
131 /*
132  * "feature-gso-tcpv4" and "feature-gso-tcpv6" advertise the capability to
133  * handle large TCP packets (in IPv4 or IPv6 form respectively). Neither
134  * frontends nor backends are assumed to be capable unless the flags are
135  * present.
136  */
137 
138 /*
139  * "feature-multicast-control" and "feature-dynamic-multicast-control"
140  * advertise the capability to filter ethernet multicast packets in the
141  * backend. If the frontend wishes to take advantage of this feature then
142  * it may set "request-multicast-control". If the backend only advertises
143  * "feature-multicast-control" then "request-multicast-control" must be set
144  * before the frontend moves into the connected state. The backend will
145  * sample the value on this state transition and any subsequent change in
146  * value will have no effect. However, if the backend also advertises
147  * "feature-dynamic-multicast-control" then "request-multicast-control"
148  * may be set by the frontend at any time. In this case, the backend will
149  * watch the value and re-sample on watch events.
150  *
151  * If the sampled value of "request-multicast-control" is set then the
152  * backend transmit side should no longer flood multicast packets to the
153  * frontend, it should instead drop any multicast packet that does not
154  * match in a filter list.
155  * The list is amended by the frontend by sending dummy transmit requests
156  * containing XEN_NETIF_EXTRA_TYPE_MCAST_{ADD,DEL} extra-info fragments as
157  * specified below.
158  * Note that the filter list may be amended even if the sampled value of
159  * "request-multicast-control" is not set, however the filter should only
160  * be applied if it is set.
161  */
162 
163 /*
164  * Control ring
165  * ============
166  *
167  * Some features, such as hashing (detailed below), require a
168  * significant amount of out-of-band data to be passed from frontend to
169  * backend. Use of xenstore is not suitable for large quantities of data
170  * because of quota limitations and so a dedicated 'control ring' is used.
171  * The ability of the backend to use a control ring is advertised by
172  * setting:
173  *
174  * /local/domain/X/backend/<domid>/<vif>/feature-ctrl-ring = "1"
175  *
176  * The frontend provides a control ring to the backend by setting:
177  *
178  * /local/domain/<domid>/device/vif/<vif>/ctrl-ring-ref = <gref>
179  * /local/domain/<domid>/device/vif/<vif>/event-channel-ctrl = <port>
180  *
181  * where <gref> is the grant reference of the shared page used to
182  * implement the control ring and <port> is an event channel to be used
183  * as a mailbox interrupt. These keys must be set before the frontend
184  * moves into the connected state.
185  *
186  * The control ring uses a fixed request/response message size and is
187  * balanced (i.e. one request to one response), so operationally it is much
188  * the same as a transmit or receive ring.
189  * Note that there is no requirement that responses are issued in the same
190  * order as requests.
191  */
192 
193 /*
194  * Hash types
195  * ==========
196  *
197  * For the purposes of the definitions below, 'Packet[]' is an array of
198  * octets containing an IP packet without options, 'Array[X..Y]' means a
199  * sub-array of 'Array' containing bytes X thru Y inclusive, and '+' is
200  * used to indicate concatenation of arrays.
201  */
202 
203 /*
204  * A hash calculated over an IP version 4 header as follows:
205  *
206  * Buffer[0..8] = Packet[12..15] (source address) +
207  *                Packet[16..19] (destination address)
208  *
209  * Result = Hash(Buffer, 8)
210  */
211 #define _XEN_NETIF_CTRL_HASH_TYPE_IPV4 0
212 #define XEN_NETIF_CTRL_HASH_TYPE_IPV4 \
213     (1 << _XEN_NETIF_CTRL_HASH_TYPE_IPV4)
214 
215 /*
216  * A hash calculated over an IP version 4 header and TCP header as
217  * follows:
218  *
219  * Buffer[0..12] = Packet[12..15] (source address) +
220  *                 Packet[16..19] (destination address) +
221  *                 Packet[20..21] (source port) +
222  *                 Packet[22..23] (destination port)
223  *
224  * Result = Hash(Buffer, 12)
225  */
226 #define _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP 1
227 #define XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP \
228     (1 << _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP)
229 
230 /*
231  * A hash calculated over an IP version 6 header as follows:
232  *
233  * Buffer[0..32] = Packet[8..23]  (source address ) +
234  *                 Packet[24..39] (destination address)
235  *
236  * Result = Hash(Buffer, 32)
237  */
238 #define _XEN_NETIF_CTRL_HASH_TYPE_IPV6 2
239 #define XEN_NETIF_CTRL_HASH_TYPE_IPV6 \
240     (1 << _XEN_NETIF_CTRL_HASH_TYPE_IPV6)
241 
242 /*
243  * A hash calculated over an IP version 6 header and TCP header as
244  * follows:
245  *
246  * Buffer[0..36] = Packet[8..23]  (source address) +
247  *                 Packet[24..39] (destination address) +
248  *                 Packet[40..41] (source port) +
249  *                 Packet[42..43] (destination port)
250  *
251  * Result = Hash(Buffer, 36)
252  */
253 #define _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP 3
254 #define XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP \
255     (1 << _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP)
256 
257 /*
258  * Hash algorithms
259  * ===============
260  */
261 
262 #define XEN_NETIF_CTRL_HASH_ALGORITHM_NONE 0
263 
264 /*
265  * Toeplitz hash:
266  */
267 
268 #define XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ 1
269 
270 /*
271  * This algorithm uses a 'key' as well as the data buffer itself.
272  * (Buffer[] and Key[] are treated as shift-registers where the MSB of
273  * Buffer/Key[0] is considered 'left-most' and the LSB of Buffer/Key[N-1]
274  * is the 'right-most').
275  *
276  * Value = 0
277  * For number of bits in Buffer[]
278  *    If (left-most bit of Buffer[] is 1)
279  *        Value ^= left-most 32 bits of Key[]
280  *    Key[] << 1
281  *    Buffer[] << 1
282  *
283  * The code below is provided for convenience where an operating system
284  * does not already provide an implementation.
285  */
286 #ifdef XEN_NETIF_DEFINE_TOEPLITZ
xen_netif_toeplitz_hash(const uint8_t * key,unsigned int keylen,const uint8_t * buf,unsigned int buflen)287 static uint32_t xen_netif_toeplitz_hash(const uint8_t *key,
288                                         unsigned int keylen,
289                                         const uint8_t *buf,
290                                         unsigned int buflen)
291 {
292     unsigned int keyi, bufi;
293     uint64_t prefix = 0;
294     uint64_t hash = 0;
295 
296     /* Pre-load prefix with the first 8 bytes of the key */
297     for (keyi = 0; keyi < 8; keyi++) {
298         prefix <<= 8;
299         prefix |= (keyi < keylen) ? key[keyi] : 0;
300     }
301 
302     for (bufi = 0; bufi < buflen; bufi++) {
303         uint8_t byte = buf[bufi];
304         unsigned int bit;
305 
306         for (bit = 0; bit < 8; bit++) {
307             if (byte & 0x80)
308                 hash ^= prefix;
309             prefix <<= 1;
310             byte <<=1;
311         }
312 
313         /*
314          * 'prefix' has now been left-shifted by 8, so
315          * OR in the next byte.
316          */
317         prefix |= (keyi < keylen) ? key[keyi] : 0;
318         keyi++;
319     }
320 
321     /* The valid part of the hash is in the upper 32 bits. */
322     return hash >> 32;
323 }
324 #endif /* XEN_NETIF_DEFINE_TOEPLITZ */
325 
326 /*
327  * Control requests (struct xen_netif_ctrl_request)
328  * ================================================
329  *
330  * All requests have the following format:
331  *
332  *    0     1     2     3     4     5     6     7  octet
333  * +-----+-----+-----+-----+-----+-----+-----+-----+
334  * |    id     |   type    |         data[0]       |
335  * +-----+-----+-----+-----+-----+-----+-----+-----+
336  * |         data[1]       |         data[2]       |
337  * +-----+-----+-----+-----+-----------------------+
338  *
339  * id: the request identifier, echoed in response.
340  * type: the type of request (see below)
341  * data[]: any data associated with the request (determined by type)
342  */
343 
344 struct xen_netif_ctrl_request {
345     uint16_t id;
346     uint16_t type;
347 
348 #define XEN_NETIF_CTRL_TYPE_INVALID               0
349 #define XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS        1
350 #define XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS        2
351 #define XEN_NETIF_CTRL_TYPE_SET_HASH_KEY          3
352 #define XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE 4
353 #define XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE 5
354 #define XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING      6
355 #define XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM    7
356 #define XEN_NETIF_CTRL_TYPE_GET_GREF_MAPPING_SIZE 8
357 #define XEN_NETIF_CTRL_TYPE_ADD_GREF_MAPPING      9
358 #define XEN_NETIF_CTRL_TYPE_DEL_GREF_MAPPING     10
359 
360     uint32_t data[3];
361 };
362 
363 /*
364  * Control responses (struct xen_netif_ctrl_response)
365  * ==================================================
366  *
367  * All responses have the following format:
368  *
369  *    0     1     2     3     4     5     6     7  octet
370  * +-----+-----+-----+-----+-----+-----+-----+-----+
371  * |    id     |   type    |         status        |
372  * +-----+-----+-----+-----+-----+-----+-----+-----+
373  * |         data          |
374  * +-----+-----+-----+-----+
375  *
376  * id: the corresponding request identifier
377  * type: the type of the corresponding request
378  * status: the status of request processing
379  * data: any data associated with the response (determined by type and
380  *       status)
381  */
382 
383 struct xen_netif_ctrl_response {
384     uint16_t id;
385     uint16_t type;
386     uint32_t status;
387 
388 #define XEN_NETIF_CTRL_STATUS_SUCCESS           0
389 #define XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     1
390 #define XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER 2
391 #define XEN_NETIF_CTRL_STATUS_BUFFER_OVERFLOW   3
392 
393     uint32_t data;
394 };
395 
396 /*
397  * Static Grants (struct xen_netif_gref)
398  * =====================================
399  *
400  * A frontend may provide a fixed set of grant references to be mapped on
401  * the backend. The message of type XEN_NETIF_CTRL_TYPE_ADD_GREF_MAPPING
402  * prior its usage in the command ring allows for creation of these mappings.
403  * The backend will maintain a fixed amount of these mappings.
404  *
405  * XEN_NETIF_CTRL_TYPE_GET_GREF_MAPPING_SIZE lets a frontend query how many
406  * of these mappings can be kept.
407  *
408  * Each entry in the XEN_NETIF_CTRL_TYPE_{ADD,DEL}_GREF_MAPPING input table has
409  * the following format:
410  *
411  *    0     1     2     3     4     5     6     7  octet
412  * +-----+-----+-----+-----+-----+-----+-----+-----+
413  * | grant ref             |  flags    |  status   |
414  * +-----+-----+-----+-----+-----+-----+-----+-----+
415  *
416  * grant ref: grant reference (IN)
417  * flags: flags describing the control operation (IN)
418  * status: XEN_NETIF_CTRL_STATUS_* (OUT)
419  *
420  * 'status' is an output parameter which does not require to be set to zero
421  * prior to its usage in the corresponding control messages.
422  */
423 
424 struct xen_netif_gref {
425        grant_ref_t ref;
426        uint16_t flags;
427 
428 #define _XEN_NETIF_CTRLF_GREF_readonly    0
429 #define XEN_NETIF_CTRLF_GREF_readonly    (1U<<_XEN_NETIF_CTRLF_GREF_readonly)
430 
431        uint16_t status;
432 };
433 
434 /*
435  * Control messages
436  * ================
437  *
438  * XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM
439  * --------------------------------------
440  *
441  * This is sent by the frontend to set the desired hash algorithm.
442  *
443  * Request:
444  *
445  *  type    = XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM
446  *  data[0] = a XEN_NETIF_CTRL_HASH_ALGORITHM_* value
447  *  data[1] = 0
448  *  data[2] = 0
449  *
450  * Response:
451  *
452  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
453  *                                                     supported
454  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - The algorithm is not
455  *                                                     supported
456  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
457  *
458  * NOTE: Setting data[0] to XEN_NETIF_CTRL_HASH_ALGORITHM_NONE disables
459  *       hashing and the backend is free to choose how it steers packets
460  *       to queues (which is the default behaviour).
461  *
462  * XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS
463  * ----------------------------------
464  *
465  * This is sent by the frontend to query the types of hash supported by
466  * the backend.
467  *
468  * Request:
469  *
470  *  type    = XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS
471  *  data[0] = 0
472  *  data[1] = 0
473  *  data[2] = 0
474  *
475  * Response:
476  *
477  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED - Operation not supported
478  *           XEN_NETIF_CTRL_STATUS_SUCCESS       - Operation successful
479  *  data   = supported hash types (if operation was successful)
480  *
481  * NOTE: A valid hash algorithm must be selected before this operation can
482  *       succeed.
483  *
484  * XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS
485  * ----------------------------------
486  *
487  * This is sent by the frontend to set the types of hash that the backend
488  * should calculate. (See above for hash type definitions).
489  * Note that the 'maximal' type of hash should always be chosen. For
490  * example, if the frontend sets both IPV4 and IPV4_TCP hash types then
491  * the latter hash type should be calculated for any TCP packet and the
492  * former only calculated for non-TCP packets.
493  *
494  * Request:
495  *
496  *  type    = XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS
497  *  data[0] = bitwise OR of XEN_NETIF_CTRL_HASH_TYPE_* values
498  *  data[1] = 0
499  *  data[2] = 0
500  *
501  * Response:
502  *
503  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
504  *                                                     supported
505  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - One or more flag
506  *                                                     value is invalid or
507  *                                                     unsupported
508  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
509  *  data   = 0
510  *
511  * NOTE: A valid hash algorithm must be selected before this operation can
512  *       succeed.
513  *       Also, setting data[0] to zero disables hashing and the backend
514  *       is free to choose how it steers packets to queues.
515  *
516  * XEN_NETIF_CTRL_TYPE_SET_HASH_KEY
517  * --------------------------------
518  *
519  * This is sent by the frontend to set the key of the hash if the algorithm
520  * requires it. (See hash algorithms above).
521  *
522  * Request:
523  *
524  *  type    = XEN_NETIF_CTRL_TYPE_SET_HASH_KEY
525  *  data[0] = grant reference of page containing the key (assumed to
526  *            start at beginning of grant)
527  *  data[1] = size of key in octets
528  *  data[2] = 0
529  *
530  * Response:
531  *
532  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
533  *                                                     supported
534  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - Key size is invalid
535  *           XEN_NETIF_CTRL_STATUS_BUFFER_OVERFLOW   - Key size is larger
536  *                                                     than the backend
537  *                                                     supports
538  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
539  *  data   = 0
540  *
541  * NOTE: Any key octets not specified are assumed to be zero (the key
542  *       is assumed to be empty by default) and specifying a new key
543  *       invalidates any previous key, hence specifying a key size of
544  *       zero will clear the key (which ensures that the calculated hash
545  *       will always be zero).
546  *       The maximum size of key is algorithm and backend specific, but
547  *       is also limited by the single grant reference.
548  *       The grant reference may be read-only and must remain valid until
549  *       the response has been processed.
550  *
551  * XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE
552  * -----------------------------------------
553  *
554  * This is sent by the frontend to query the maximum size of mapping
555  * table supported by the backend. The size is specified in terms of
556  * table entries.
557  *
558  * Request:
559  *
560  *  type    = XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE
561  *  data[0] = 0
562  *  data[1] = 0
563  *  data[2] = 0
564  *
565  * Response:
566  *
567  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED - Operation not supported
568  *           XEN_NETIF_CTRL_STATUS_SUCCESS       - Operation successful
569  *  data   = maximum number of entries allowed in the mapping table
570  *           (if operation was successful) or zero if a mapping table is
571  *           not supported (i.e. hash mapping is done only by modular
572  *           arithmetic).
573  *
574  * XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE
575  * -------------------------------------
576  *
577  * This is sent by the frontend to set the actual size of the mapping
578  * table to be used by the backend. The size is specified in terms of
579  * table entries.
580  * Any previous table is invalidated by this message and any new table
581  * is assumed to be zero filled.
582  *
583  * Request:
584  *
585  *  type    = XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE
586  *  data[0] = number of entries in mapping table
587  *  data[1] = 0
588  *  data[2] = 0
589  *
590  * Response:
591  *
592  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
593  *                                                     supported
594  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - Table size is invalid
595  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
596  *  data   = 0
597  *
598  * NOTE: Setting data[0] to 0 means that hash mapping should be done
599  *       using modular arithmetic.
600  *
601  * XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING
602  * ------------------------------------
603  *
604  * This is sent by the frontend to set the content of the table mapping
605  * hash value to queue number. The backend should calculate the hash from
606  * the packet header, use it as an index into the table (modulo the size
607  * of the table) and then steer the packet to the queue number found at
608  * that index.
609  *
610  * Request:
611  *
612  *  type    = XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING
613  *  data[0] = grant reference of page containing the mapping (sub-)table
614  *            (assumed to start at beginning of grant)
615  *  data[1] = size of (sub-)table in entries
616  *  data[2] = offset, in entries, of sub-table within overall table
617  *
618  * Response:
619  *
620  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
621  *                                                     supported
622  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - Table size or content
623  *                                                     is invalid
624  *           XEN_NETIF_CTRL_STATUS_BUFFER_OVERFLOW   - Table size is larger
625  *                                                     than the backend
626  *                                                     supports
627  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
628  *  data   = 0
629  *
630  * NOTE: The overall table has the following format:
631  *
632  *          0     1     2     3     4     5     6     7  octet
633  *       +-----+-----+-----+-----+-----+-----+-----+-----+
634  *       |       mapping[0]      |       mapping[1]      |
635  *       +-----+-----+-----+-----+-----+-----+-----+-----+
636  *       |                       .                       |
637  *       |                       .                       |
638  *       |                       .                       |
639  *       +-----+-----+-----+-----+-----+-----+-----+-----+
640  *       |      mapping[N-2]     |      mapping[N-1]     |
641  *       +-----+-----+-----+-----+-----+-----+-----+-----+
642  *
643  *       where N is specified by a XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE
644  *       message and each  mapping must specifies a queue between 0 and
645  *       "multi-queue-num-queues" (see above).
646  *       The backend may support a mapping table larger than can be
647  *       mapped by a single grant reference. Thus sub-tables within a
648  *       larger table can be individually set by sending multiple messages
649  *       with differing offset values. Specifying a new sub-table does not
650  *       invalidate any table data outside that range.
651  *       The grant reference may be read-only and must remain valid until
652  *       the response has been processed.
653  *
654  * XEN_NETIF_CTRL_TYPE_GET_GREF_MAPPING_SIZE
655  * -----------------------------------------
656  *
657  * This is sent by the frontend to fetch the number of grefs that can be kept
658  * mapped in the backend.
659  *
660  * Request:
661  *
662  *  type    = XEN_NETIF_CTRL_TYPE_GET_GREF_MAPPING_SIZE
663  *  data[0] = queue index (assumed 0 for single queue)
664  *  data[1] = 0
665  *  data[2] = 0
666  *
667  * Response:
668  *
669  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
670  *                                                     supported
671  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - The queue index is
672  *                                                     out of range
673  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
674  *  data   = maximum number of entries allowed in the gref mapping table
675  *           (if operation was successful) or zero if it is not supported.
676  *
677  * XEN_NETIF_CTRL_TYPE_ADD_GREF_MAPPING
678  * ------------------------------------
679  *
680  * This is sent by the frontend for backend to map a list of grant
681  * references.
682  *
683  * Request:
684  *
685  *  type    = XEN_NETIF_CTRL_TYPE_ADD_GREF_MAPPING
686  *  data[0] = queue index
687  *  data[1] = grant reference of page containing the mapping list
688  *            (r/w and assumed to start at beginning of page)
689  *  data[2] = size of list in entries
690  *
691  * Response:
692  *
693  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
694  *                                                     supported
695  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - Operation failed
696  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
697  *
698  * NOTE: Each entry in the input table has the format outlined
699  *       in struct xen_netif_gref.
700  *       Contrary to XEN_NETIF_CTRL_TYPE_DEL_GREF_MAPPING, the struct
701  *       xen_netif_gref 'status' field is not used and therefore the response
702  *       'status' determines the success of this operation. In case of
703  *       failure none of grants mappings get added in the backend.
704  *
705  * XEN_NETIF_CTRL_TYPE_DEL_GREF_MAPPING
706  * ------------------------------------
707  *
708  * This is sent by the frontend for backend to unmap a list of grant
709  * references.
710  *
711  * Request:
712  *
713  *  type    = XEN_NETIF_CTRL_TYPE_DEL_GREF_MAPPING
714  *  data[0] = queue index
715  *  data[1] = grant reference of page containing the mapping list
716  *            (r/w and assumed to start at beginning of page)
717  *  data[2] = size of list in entries
718  *
719  * Response:
720  *
721  *  status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED     - Operation not
722  *                                                     supported
723  *           XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER - Operation failed
724  *           XEN_NETIF_CTRL_STATUS_SUCCESS           - Operation successful
725  *  data   = number of entries that were unmapped
726  *
727  * NOTE: Each entry in the input table has the format outlined in struct
728  *       xen_netif_gref.
729  *       The struct xen_netif_gref 'status' field determines if the entry
730  *       was successfully removed.
731  *       The entries used are only the ones representing grant references that
732  *       were previously the subject of a XEN_NETIF_CTRL_TYPE_ADD_GREF_MAPPING
733  *       operation. Any other entries will have their status set to
734  *       XEN_NETIF_CTRL_STATUS_INVALID_PARAMETER upon completion.
735  */
736 
737 DEFINE_RING_TYPES(xen_netif_ctrl,
738                   struct xen_netif_ctrl_request,
739                   struct xen_netif_ctrl_response);
740 
741 /*
742  * Guest transmit
743  * ==============
744  *
745  * This is the 'wire' format for transmit (frontend -> backend) packets:
746  *
747  *  Fragment 1: netif_tx_request_t  - flags = NETTXF_*
748  *                                    size = total packet size
749  * [Extra 1: netif_extra_info_t]    - (only if fragment 1 flags include
750  *                                     NETTXF_extra_info)
751  *  ...
752  * [Extra N: netif_extra_info_t]    - (only if extra N-1 flags include
753  *                                     XEN_NETIF_EXTRA_MORE)
754  *  ...
755  *  Fragment N: netif_tx_request_t  - (only if fragment N-1 flags include
756  *                                     NETTXF_more_data - flags on preceding
757  *                                     extras are not relevant here)
758  *                                    flags = 0
759  *                                    size = fragment size
760  *
761  * NOTE:
762  *
763  * This format slightly is different from that used for receive
764  * (backend -> frontend) packets. Specifically, in a multi-fragment
765  * packet the actual size of fragment 1 can only be determined by
766  * subtracting the sizes of fragments 2..N from the total packet size.
767  *
768  * Ring slot size is 12 octets, however not all request/response
769  * structs use the full size.
770  *
771  * tx request data (netif_tx_request_t)
772  * ------------------------------------
773  *
774  *    0     1     2     3     4     5     6     7  octet
775  * +-----+-----+-----+-----+-----+-----+-----+-----+
776  * | grant ref             | offset    | flags     |
777  * +-----+-----+-----+-----+-----+-----+-----+-----+
778  * | id        | size      |
779  * +-----+-----+-----+-----+
780  *
781  * grant ref: Reference to buffer page.
782  * offset: Offset within buffer page.
783  * flags: NETTXF_*.
784  * id: request identifier, echoed in response.
785  * size: packet size in bytes.
786  *
787  * tx response (netif_tx_response_t)
788  * ---------------------------------
789  *
790  *    0     1     2     3     4     5     6     7  octet
791  * +-----+-----+-----+-----+-----+-----+-----+-----+
792  * | id        | status    | unused                |
793  * +-----+-----+-----+-----+-----+-----+-----+-----+
794  * | unused                |
795  * +-----+-----+-----+-----+
796  *
797  * id: reflects id in transmit request
798  * status: NETIF_RSP_*
799  *
800  * Guest receive
801  * =============
802  *
803  * This is the 'wire' format for receive (backend -> frontend) packets:
804  *
805  *  Fragment 1: netif_rx_request_t  - flags = NETRXF_*
806  *                                    size = fragment size
807  * [Extra 1: netif_extra_info_t]    - (only if fragment 1 flags include
808  *                                     NETRXF_extra_info)
809  *  ...
810  * [Extra N: netif_extra_info_t]    - (only if extra N-1 flags include
811  *                                     XEN_NETIF_EXTRA_MORE)
812  *  ...
813  *  Fragment N: netif_rx_request_t  - (only if fragment N-1 flags include
814  *                                     NETRXF_more_data - flags on preceding
815  *                                     extras are not relevant here)
816  *                                    flags = 0
817  *                                    size = fragment size
818  *
819  * NOTE:
820  *
821  * This format slightly is different from that used for transmit
822  * (frontend -> backend) packets. Specifically, in a multi-fragment
823  * packet the size of the packet can only be determined by summing the
824  * sizes of fragments 1..N.
825  *
826  * Ring slot size is 8 octets.
827  *
828  * rx request (netif_rx_request_t)
829  * -------------------------------
830  *
831  *    0     1     2     3     4     5     6     7  octet
832  * +-----+-----+-----+-----+-----+-----+-----+-----+
833  * | id        | pad       | gref                  |
834  * +-----+-----+-----+-----+-----+-----+-----+-----+
835  *
836  * id: request identifier, echoed in response.
837  * gref: reference to incoming granted frame.
838  *
839  * rx response (netif_rx_response_t)
840  * ---------------------------------
841  *
842  *    0     1     2     3     4     5     6     7  octet
843  * +-----+-----+-----+-----+-----+-----+-----+-----+
844  * | id        | offset    | flags     | status    |
845  * +-----+-----+-----+-----+-----+-----+-----+-----+
846  *
847  * id: reflects id in receive request
848  * offset: offset in page of start of received packet
849  * flags: NETRXF_*
850  * status: -ve: NETIF_RSP_*; +ve: Rx'ed pkt size.
851  *
852  * NOTE: Historically, to support GSO on the frontend receive side, Linux
853  *       netfront does not make use of the rx response id (because, as
854  *       described below, extra info structures overlay the id field).
855  *       Instead it assumes that responses always appear in the same ring
856  *       slot as their corresponding request. Thus, to maintain
857  *       compatibility, backends must make sure this is the case.
858  *
859  * Extra Info
860  * ==========
861  *
862  * Can be present if initial request or response has NET{T,R}XF_extra_info,
863  * or previous extra request has XEN_NETIF_EXTRA_MORE.
864  *
865  * The struct therefore needs to fit into either a tx or rx slot and
866  * is therefore limited to 8 octets.
867  *
868  * NOTE: Because extra info data overlays the usual request/response
869  *       structures, there is no id information in the opposite direction.
870  *       So, if an extra info overlays an rx response the frontend can
871  *       assume that it is in the same ring slot as the request that was
872  *       consumed to make the slot available, and the backend must ensure
873  *       this assumption is true.
874  *
875  * extra info (netif_extra_info_t)
876  * -------------------------------
877  *
878  * General format:
879  *
880  *    0     1     2     3     4     5     6     7  octet
881  * +-----+-----+-----+-----+-----+-----+-----+-----+
882  * |type |flags| type specific data                |
883  * +-----+-----+-----+-----+-----+-----+-----+-----+
884  * | padding for tx        |
885  * +-----+-----+-----+-----+
886  *
887  * type: XEN_NETIF_EXTRA_TYPE_*
888  * flags: XEN_NETIF_EXTRA_FLAG_*
889  * padding for tx: present only in the tx case due to 8 octet limit
890  *                 from rx case. Not shown in type specific entries
891  *                 below.
892  *
893  * XEN_NETIF_EXTRA_TYPE_GSO:
894  *
895  *    0     1     2     3     4     5     6     7  octet
896  * +-----+-----+-----+-----+-----+-----+-----+-----+
897  * |type |flags| size      |type | pad | features  |
898  * +-----+-----+-----+-----+-----+-----+-----+-----+
899  *
900  * type: Must be XEN_NETIF_EXTRA_TYPE_GSO
901  * flags: XEN_NETIF_EXTRA_FLAG_*
902  * size: Maximum payload size of each segment. For example,
903  *       for TCP this is just the path MSS.
904  * type: XEN_NETIF_GSO_TYPE_*: This determines the protocol of
905  *       the packet and any extra features required to segment the
906  *       packet properly.
907  * features: EN_NETIF_GSO_FEAT_*: This specifies any extra GSO
908  *           features required to process this packet, such as ECN
909  *           support for TCPv4.
910  *
911  * XEN_NETIF_EXTRA_TYPE_MCAST_{ADD,DEL}:
912  *
913  *    0     1     2     3     4     5     6     7  octet
914  * +-----+-----+-----+-----+-----+-----+-----+-----+
915  * |type |flags| addr                              |
916  * +-----+-----+-----+-----+-----+-----+-----+-----+
917  *
918  * type: Must be XEN_NETIF_EXTRA_TYPE_MCAST_{ADD,DEL}
919  * flags: XEN_NETIF_EXTRA_FLAG_*
920  * addr: address to add/remove
921  *
922  * XEN_NETIF_EXTRA_TYPE_HASH:
923  *
924  * A backend that supports teoplitz hashing is assumed to accept
925  * this type of extra info in transmit packets.
926  * A frontend that enables hashing is assumed to accept
927  * this type of extra info in receive packets.
928  *
929  *    0     1     2     3     4     5     6     7  octet
930  * +-----+-----+-----+-----+-----+-----+-----+-----+
931  * |type |flags|htype| alg |LSB ---- value ---- MSB|
932  * +-----+-----+-----+-----+-----+-----+-----+-----+
933  *
934  * type: Must be XEN_NETIF_EXTRA_TYPE_HASH
935  * flags: XEN_NETIF_EXTRA_FLAG_*
936  * htype: Hash type (one of _XEN_NETIF_CTRL_HASH_TYPE_* - see above)
937  * alg: The algorithm used to calculate the hash (one of
938  *      XEN_NETIF_CTRL_HASH_TYPE_ALGORITHM_* - see above)
939  * value: Hash value
940  */
941 
942 /* Protocol checksum field is blank in the packet (hardware offload)? */
943 #define _NETTXF_csum_blank     (0)
944 #define  NETTXF_csum_blank     (1U<<_NETTXF_csum_blank)
945 
946 /* Packet data has been validated against protocol checksum. */
947 #define _NETTXF_data_validated (1)
948 #define  NETTXF_data_validated (1U<<_NETTXF_data_validated)
949 
950 /* Packet continues in the next request descriptor. */
951 #define _NETTXF_more_data      (2)
952 #define  NETTXF_more_data      (1U<<_NETTXF_more_data)
953 
954 /* Packet to be followed by extra descriptor(s). */
955 #define _NETTXF_extra_info     (3)
956 #define  NETTXF_extra_info     (1U<<_NETTXF_extra_info)
957 
958 #define XEN_NETIF_MAX_TX_SIZE 0xFFFF
959 struct netif_tx_request {
960     grant_ref_t gref;
961     uint16_t offset;
962     uint16_t flags;
963     uint16_t id;
964     uint16_t size;
965 };
966 typedef struct netif_tx_request netif_tx_request_t;
967 
968 /* Types of netif_extra_info descriptors. */
969 #define XEN_NETIF_EXTRA_TYPE_NONE      (0)  /* Never used - invalid */
970 #define XEN_NETIF_EXTRA_TYPE_GSO       (1)  /* u.gso */
971 #define XEN_NETIF_EXTRA_TYPE_MCAST_ADD (2)  /* u.mcast */
972 #define XEN_NETIF_EXTRA_TYPE_MCAST_DEL (3)  /* u.mcast */
973 #define XEN_NETIF_EXTRA_TYPE_HASH      (4)  /* u.hash */
974 #define XEN_NETIF_EXTRA_TYPE_MAX       (5)
975 
976 /* netif_extra_info_t flags. */
977 #define _XEN_NETIF_EXTRA_FLAG_MORE (0)
978 #define XEN_NETIF_EXTRA_FLAG_MORE  (1U<<_XEN_NETIF_EXTRA_FLAG_MORE)
979 
980 /* GSO types */
981 #define XEN_NETIF_GSO_TYPE_NONE         (0)
982 #define XEN_NETIF_GSO_TYPE_TCPV4        (1)
983 #define XEN_NETIF_GSO_TYPE_TCPV6        (2)
984 
985 /*
986  * This structure needs to fit within both netif_tx_request_t and
987  * netif_rx_response_t for compatibility.
988  */
989 struct netif_extra_info {
990     uint8_t type;
991     uint8_t flags;
992     union {
993         struct {
994             uint16_t size;
995             uint8_t type;
996             uint8_t pad;
997             uint16_t features;
998         } gso;
999         struct {
1000             uint8_t addr[6];
1001         } mcast;
1002         struct {
1003             uint8_t type;
1004             uint8_t algorithm;
1005             uint8_t value[4];
1006         } hash;
1007         uint16_t pad[3];
1008     } u;
1009 };
1010 typedef struct netif_extra_info netif_extra_info_t;
1011 
1012 struct netif_tx_response {
1013     uint16_t id;
1014     int16_t  status;
1015 };
1016 typedef struct netif_tx_response netif_tx_response_t;
1017 
1018 struct netif_rx_request {
1019     uint16_t    id;        /* Echoed in response message.        */
1020     uint16_t    pad;
1021     grant_ref_t gref;
1022 };
1023 typedef struct netif_rx_request netif_rx_request_t;
1024 
1025 /* Packet data has been validated against protocol checksum. */
1026 #define _NETRXF_data_validated (0)
1027 #define  NETRXF_data_validated (1U<<_NETRXF_data_validated)
1028 
1029 /* Protocol checksum field is blank in the packet (hardware offload)? */
1030 #define _NETRXF_csum_blank     (1)
1031 #define  NETRXF_csum_blank     (1U<<_NETRXF_csum_blank)
1032 
1033 /* Packet continues in the next request descriptor. */
1034 #define _NETRXF_more_data      (2)
1035 #define  NETRXF_more_data      (1U<<_NETRXF_more_data)
1036 
1037 /* Packet to be followed by extra descriptor(s). */
1038 #define _NETRXF_extra_info     (3)
1039 #define  NETRXF_extra_info     (1U<<_NETRXF_extra_info)
1040 
1041 /* Packet has GSO prefix. Deprecated but included for compatibility */
1042 #define _NETRXF_gso_prefix     (4)
1043 #define  NETRXF_gso_prefix     (1U<<_NETRXF_gso_prefix)
1044 
1045 struct netif_rx_response {
1046     uint16_t id;
1047     uint16_t offset;
1048     uint16_t flags;
1049     int16_t  status;
1050 };
1051 typedef struct netif_rx_response netif_rx_response_t;
1052 
1053 /*
1054  * Generate netif ring structures and types.
1055  */
1056 
1057 DEFINE_RING_TYPES(netif_tx, struct netif_tx_request, struct netif_tx_response);
1058 DEFINE_RING_TYPES(netif_rx, struct netif_rx_request, struct netif_rx_response);
1059 
1060 #define NETIF_RSP_DROPPED         -2
1061 #define NETIF_RSP_ERROR           -1
1062 #define NETIF_RSP_OKAY             0
1063 /* No response: used for auxiliary requests (e.g., netif_extra_info_t). */
1064 #define NETIF_RSP_NULL             1
1065 
1066 #endif
1067 
1068 /*
1069  * Local variables:
1070  * mode: C
1071  * c-file-style: "BSD"
1072  * c-basic-offset: 4
1073  * tab-width: 4
1074  * indent-tabs-mode: nil
1075  * End:
1076  */
1077