]> git.proxmox.com Git - mirror_iproute2.git/blob - man/man8/tc-u32.8
ip-xfrm: Add support for OUTPUT_MARK
[mirror_iproute2.git] / man / man8 / tc-u32.8
1 .TH "Universal 32bit classifier in tc" 8 "25 Sep 2015" "iproute2" "Linux"
2
3 .SH NAME
4 u32 \- universal 32bit traffic control filter
5 .SH SYNOPSIS
6 .in +8
7 .ti -8
8 .BR tc " " filter " ... [ " handle
9 .IR HANDLE " ] "
10 .B u32
11 .IR OPTION_LIST " [ "
12 .B offset
13 .IR OFFSET " ] [ "
14 .B hashkey
15 .IR HASHKEY " ] [ "
16 .B classid
17 .IR CLASSID " ] [ "
18 .B divisor
19 .IR uint_value " ] [ "
20 .B order
21 .IR u32_value " ] [ "
22 .B ht
23 .IR HANDLE " ] [ "
24 .B sample
25 .IR SELECTOR " [ "
26 .B divisor
27 .IR uint_value " ] ] [ "
28 .B link
29 .IR HANDLE " ] [ "
30 .B indev
31 .IR ifname " ] [ "
32 .BR skip_hw " | "
33 .BR skip_sw " ] [ "
34 .BR help " ]"
35
36 .ti -8
37 .IR HANDLE " := { "
38 \fIu12_hex_htid\fB:\fR[\fIu8_hex_hash\fB:\fR[\fIu12_hex_nodeid\fR] | \fB0x\fIu32_hex_value\fR }
39
40 .ti -8
41 .IR OPTION_LIST " := [ " OPTION_LIST " ] " OPTION
42
43 .ti -8
44 .IR HASHKEY " := [ "
45 .B mask
46 .IR u32_hex_value " ] [ "
47 .B at
48 .IR 4*int_value " ]"
49
50 .ti -8
51 .IR CLASSID " := { "
52 .BR root " | "
53 .BR none " | "
54 [\fIu16_major\fR]\fB:\fIu16_minor\fR | \fIu32_hex_value\fR }
55
56 .ti -8
57 .IR OFFSET " := [ "
58 .B plus
59 .IR int_value " ] [ "
60 .B at
61 .IR 2*int_value " ] [ "
62 .B mask
63 .IR u16_hex_value " ] [ "
64 .B shift
65 .IR int_value " ] [ "
66 .BR eat " ]"
67
68 .ti -8
69 .IR OPTION " := { "
70 .B match
71 .IR SELECTOR " | "
72 .B action
73 .IR ACTION " } "
74
75 .ti -8
76 .IR SELECTOR " := { "
77 .B u32
78 .IR VAL_MASK_32 " | "
79 .B u16
80 .IR VAL_MASK_16 " | "
81 .B u8
82 .IR VAL_MASK_8 " | "
83 .B ip
84 .IR IP " | "
85 .B ip6
86 .IR IP6 " | { "
87 .BR tcp " | " udp " } "
88 .IR TCPUDP " | "
89 .B icmp
90 .IR ICMP " | "
91 .B mark
92 .IR VAL_MASK_32 " | "
93 .B ether
94 .IR ETHER " }"
95
96 .ti -8
97 .IR IP " := { { "
98 .BR src " | " dst " } { " default " | " any " | " all " | "
99 .IR ip_address " [ "
100 .BR / " { "
101 .IR prefixlen " | " netmask " } ] } " AT " | { "
102 .BR dsfield " | " ihl " | " protocol " | " precedence " | "
103 .BR icmp_type " | " icmp_code " } "
104 .IR VAL_MASK_8 " | { "
105 .BR sport " | " dport " } "
106 .IR VAL_MASK_16 " | "
107 .BR nofrag " | " firstfrag " | " df " | " mf " }"
108
109 .ti -8
110 .IR IP6 " := { { "
111 .BR src " | " dst " } { " default " | " any " | " all " | "
112 .IR ip6_address " [/" prefixlen " ] } " AT " | "
113 .B priority
114 .IR VAL_MASK_8 " | { "
115 .BR protocol " | " icmp_type " | " icmp_code " } "
116 .IR VAL_MASK_8 " | "
117 .B flowlabel
118 .IR VAL_MASK_32 " | { "
119 .BR sport " | " dport " } "
120 .IR VAL_MASK_16 " }"
121
122 .ti -8
123 .IR TCPUDP " := { "
124 .BR src " | " dst " } "
125 .I VAL_MASK_16
126
127 .ti -8
128 .IR ICMP " := { "
129 .B type
130 .IR VAL_MASK_8 " | "
131 .B code
132 .IR VAL_MASK_8 " }"
133
134 .ti -8
135 .IR ETHER " := { "
136 .BR src " | " dst " } "
137 .IR ether_address " " AT
138
139 .ti -8
140 .IR VAL_MASK_32 " := " u32_value " " u32_hex_mask " [ " AT " ]"
141
142 .ti -8
143 .IR VAL_MASK_16 " := " u16_value " " u16_hex_mask " [ " AT " ]"
144
145 .ti -8
146 .IR VAL_MASK_8 " := " u8_value " " u8_hex_mask " [ " AT " ]"
147
148 .ti -8
149 .IR AT " := [ "
150 .BR at " [ " nexthdr+ " ] "
151 .IR int_value " ]"
152 .SH DESCRIPTION
153 The Universal/Ugly 32bit filter allows to match arbitrary bitfields in the
154 packet. Due to breaking everything down to values, masks and offsets, It is
155 equally powerful and hard to use. Luckily many abstracting directives are
156 present which allow defining rules on a higher level and therefore free the
157 user from having to fiddle with bits and masks in many cases.
158
159 There are two general modes of invocation: The first mode creates a new filter
160 to delegate packets to different destinations. Apart from the obvious ones,
161 namely classifying the packet by specifying a
162 .I CLASSID
163 or calling an
164 .BR action ,
165 one may
166 .B link
167 one filter to another one (or even a list of them), effectively organizing
168 filters into a tree-like hierarchy.
169
170 Typically filter delegation is done by means of a hash table, which leads to the
171 second mode of invocation: it merely serves to set up these hash tables. Filters
172 can select a hash table and provide a key selector from which a hash is to be
173 computed and used as key to lookup the table's bucket which contains filters for
174 further processing. This is useful if a high number of filters is in use, as the
175 overhead of performing the hash operation and table lookup becomes negligible in
176 that case. Using hashtables with
177 .B u32
178 basically involves the following pattern:
179 .IP (1) 4
180 Creating a new hash table, specifying it's size using the
181 .B divisor
182 parameter and ideally a handle by which the table can be identified. If the
183 latter is not given, the kernel chooses one on it's own, which has to be
184 guessed later.
185 .IP (2) 4
186 Creating filters which link to the created table in
187 .I (1)
188 using the
189 .B link
190 parameter and defining the packet data which the kernel will use to calculate
191 the
192 .BR hashkey .
193 .IP (3) 4
194 Adding filters to buckets in the hash table from
195 .IR (1) .
196 In order to avoid having to know how exactly the kernel creates the hash key,
197 there is the
198 .B sample
199 parameter, which gives sample data to hash and thereby define the table bucket
200 the filter should be added to.
201
202 .RE
203 In fact, even if not explicitly requested
204 .B u32
205 creates a hash table for every
206 .B priority
207 a filter is being added with. The table's size is 1 though, so it is in fact
208 merely a linked list.
209 .SH VALUES
210 Options and selectors require values to be specified in a specific format, which
211 is often non-intuitive. Therefore the terminals in
212 .I SYNOPSIS
213 have been given descriptive names to indicate the required format and/or maximum
214 allowed numeric value: Prefixes
215 .IR u32 ", " u16 " and " u8
216 indicate four, two and single byte unsigned values. E.g.
217 .I u16
218 indicates a two byte-sized value in range between 0 and 65535 (0xFFFF)
219 inclusive. A prefix of
220 .I int
221 indicates a four byte signed value. A middle part of
222 .I _hex_
223 indicates that the value is parsed in hexadecimal format. Otherwise, the
224 value's base is automatically detected, i.e. values prefixed with
225 .I 0x
226 are considered hexadecimal, a leading
227 .I 0
228 indicates octal format and decimal format otherwise. There are some values with
229 special formatting as well:
230 .IR ip_address " and " netmask
231 are in dotted-quad formatting as usual for IPv4 addresses. An
232 .I ip6_address
233 is specified in common, colon-separated hexadecimal format. Finally,
234 .I prefixlen
235 is an unsigned, decimal integer value in range from 0 to the address width in
236 bits (32 for IPv4 and 128 for IPv6).
237
238 Sometimes values need to be dividable by a certain number. In that case a name
239 of the form
240 .I N*val
241 was chosen, indicating that
242 .I val
243 must be dividable by
244 .IR N .
245 Or the other way around: the resulting value must be a multiple of
246 .IR N .
247 .SH OPTIONS
248 .B U32
249 recognizes the following options:
250 .TP
251 .BI handle " HANDLE"
252 The handle is used to reference a filter and therefore must be unique. It
253 consists of a hash table identifier
254 .B htid
255 and optional
256 .B hash
257 (which identifies the hash table's bucket) and
258 .BR nodeid .
259 All these values are parsed as unsigned, hexadecimal numbers with length 12bits
260 (
261 .BR htid " and " nodeid )
262 or 8bits (
263 .BR hash ).
264 Alternatively one may specify a single, 32bit long hex number which contains
265 the three fields bits in concatenated form. Other than the fields themselves, it
266 has to be prefixed by
267 .BR 0x .
268 .TP
269 .BI offset " OFFSET"
270 Set an offset which defines where matches of subsequent filters are applied to.
271 Therefore this option is useful only when combined with
272 .BR link " or a combination of " ht " and " sample .
273 The offset may be given explicitly by using the
274 .B plus
275 keyword, or extracted from the packet data with
276 .BR at .
277 It is possible to mangle the latter using
278 .BR mask " and/or " shift
279 keywords. By default, this offset is recorded but not implicitly applied. It is
280 used only to substitute the
281 .B nexthdr+
282 statement. Using the keyword
283 .B eat
284 though inverses this behaviour: the offset is applied always, and
285 .B nexthdr+
286 will fall back to zero.
287 .TP
288 .BI hashkey " HASHKEY"
289 Spefify what packet data to use to calculate a hash key for bucket lookup. The
290 kernel adjusts the value according to the hash table's size. For this to work,
291 the option
292 .B link
293 must be given.
294 .TP
295 .BI classid " CLASSID"
296 Classify matching packets into the given
297 .IR CLASSID ,
298 which consists of either 16bit
299 .BR major " and " minor
300 numbers or a single 32bit value combining both.
301 .TP
302 .BI divisor " u32_value"
303 Specify a modulo value. Used when creating hash tables to define their size or
304 for declaring a
305 .B sample
306 to calculate hash table keys from. Must be a power of two with exponent not
307 exceeding eight.
308 .TP
309 .BI order " u32_value"
310 A value to order filters by, ascending. Conflicts with
311 .B handle
312 which serves the same purpose.
313 .TP
314 .BI sample " SELECTOR"
315 Used together with
316 .B ht
317 to specify which bucket to add this filter to. This allows one to avoid having
318 to know how exactly the kernel calculates hashes. The additional
319 .B divisor
320 defaults to 256, so must be given for hash tables of different size.
321 .TP
322 .BI link " HANDLE"
323 Delegate matching packets to filters in a hash table.
324 .I HANDLE
325 is used to only specify the hash table, so only
326 .BR htid " may be given, " hash " and " nodeid
327 have to be omitted. By default, bucket number 0 will be used and can be
328 overridden by the
329 .B hashkey
330 option.
331 .TP
332 .BI indev " ifname"
333 Filter on the incoming interface of the packet. Obviously works only for
334 forwarded traffic.
335 .TP
336 .BI skip_sw
337 Do not process filter by software. If hardware has no offload support for this
338 filter, or TC offload is not enabled for the interface, operation will fail.
339 .TP
340 .BI skip_hw
341 Do not process filter by hardware.
342 .TP
343 .BI help
344 Print a brief help text about possible options.
345 .SH SELECTORS
346 Basically the only real selector is
347 .B u32 .
348 All others merely provide a higher level syntax and are internally translated
349 into
350 .B u32 .
351 .TP
352 .BI u32 " VAL_MASK_32"
353 .TQ
354 .BI u16 " VAL_MASK_16"
355 .TQ
356 .BI u8 " VAL_MASK_8"
357 Match packet data to a given value. The selector name defines the sample length
358 to extract (32bits for
359 .BR u32 ,
360 16bits for
361 .B u16
362 and 8bits for
363 .BR u8 ).
364 Before comparing, the sample is binary AND'ed with the given mask. This way
365 uninteresting bits can be cleared before comparison. The position of the sample
366 is defined by the offset specified in
367 .IR AT .
368 .TP
369 .BI ip " IP"
370 .TQ
371 .BI ip6 " IP6"
372 Assume packet starts with an IPv4 (
373 .BR ip )
374 or IPv6 (
375 .BR ip6 )
376 header.
377 .IR IP / IP6
378 then allows to match various header fields:
379 .RS
380 .TP
381 .BI src " ADDR"
382 .TQ
383 .BI dst " ADDR"
384 Compare Source or Destination Address fields against the value of
385 .IR ADDR .
386 The reserved words
387 .BR default ", " any " and " all
388 effectively match any address. Otherwise an IP address of the particular
389 protocol is expected, optionally suffixed by a prefix length to match whole
390 subnets. In case of IPv4 a netmask may also be given.
391 .TP
392 .BI dsfield " VAL_MASK_8"
393 IPv4 only. Match the packet header's DSCP/ECN field. Synonyms to this are
394 .BR tos " and " precedence .
395 .TP
396 .BI ihl " VAL_MASK_8"
397 IPv4 only. Match the Internet Header Length field. Note that the value's unit is
398 32bits, so to match a packet with 24byte header length
399 .I u8_value
400 has to be 6.
401 .TP
402 .BI protocol " VAL_MASK_8"
403 Match the Protocol (IPv4) or Next Header (IPv6) field value, e.g. 6 for TCP.
404 .TP
405 .BI icmp_type " VAL_MASK_8"
406 .TQ
407 .BI icmp_code " VAL_MASK_8"
408 Assume a next-header protocol of icmp or ipv6-icmp and match Type or Code
409 field values. This is dangerous, as the code assumes minimal header size for
410 IPv4 and lack of extension headers for IPv6.
411 .TP
412 .BI sport " VAL_MASK_16"
413 .TQ
414 .BI dport " VAL_MASK_16"
415 Match layer four source or destination ports. This is dangerous as well, as it
416 assumes a suitable layer four protocol is present (which has Source and
417 Destination Port fields right at the start of the header and 16bit in size).
418 Also minimal header size for IPv4 and lack of IPv6 extension headers is assumed.
419 .TP
420 .B nofrag
421 .TQ
422 .B firstfrag
423 .TQ
424 .B df
425 .TQ
426 .B mf
427 IPv4 only, check certain flags and fragment offset values. Match if the packet
428 is not a fragment
429 .RB ( nofrag ),
430 the first fragment
431 .RB ( firstfrag ),
432 if Don't Fragment
433 .RB ( df )
434 or More Fragments
435 .RB ( mf )
436 bits are set.
437 .TP
438 .BI priority " VAL_MASK_8"
439 IPv6 only. Match the header's Traffic Class field, which has the same purpose
440 and semantics of IPv4's ToS field since RFC 3168: upper six bits are DSCP, the
441 lower two ECN.
442 .TP
443 .BI flowlabel " VAL_MASK_32"
444 IPv6 only. Match the Flow Label field's value. Note that Flow Label itself is
445 only 20bytes long, which are the least significant ones here. The remaining
446 upper 12bytes match Version and Traffic Class fields.
447 .RE
448 .TP
449 .BI tcp " TCPUDP"
450 .TQ
451 .BI udp " TCPUDP"
452 Match fields of next header of protocol TCP or UDP. The possible values for
453 .I TCPDUP
454 are:
455 .RS
456 .TP
457 .BI src " VAL_MASK_16"
458 Match on Source Port field value.
459 .TP
460 .BI dst " VALMASK_16"
461 Match on Destination Port field value.
462 .RE
463 .TP
464 .BI icmp " ICMP"
465 Match fields of next header of protocol ICMP. The possible values for
466 .I ICMP
467 are:
468 .RS
469 .TP
470 .BI type " VAL_MASK_8"
471 Match on ICMP Type field.
472 .TP
473 .BI code " VAL_MASK_8"
474 Match on ICMP Code field.
475 .RE
476 .TP
477 .BI mark " VAL_MASK_32"
478 Match on netfilter fwmark value.
479 .TP
480 .BI ether " ETHER"
481 Match on ethernet header fields. Possible values for
482 .I ETHER
483 are:
484 .RS
485 .TP
486 .BI src " ether_address" " " AT
487 .TQ
488 .BI dst " ether_address" " " AT
489 Match on source or destination ethernet address. This is dangerous: It assumes
490 an ethernet header is present at the start of the packet. This will probably
491 lead to unexpected things if used with layer three interfaces like e.g. tun or
492 ppp.
493 .SH EXAMPLES
494 .RS
495 .EX
496 tc filter add dev eth0 parent 999:0 prio 99 protocol ip u32 \\
497 match ip src 192.168.8.0/24 classid 1:1
498 .EE
499 .RE
500
501 This attaches a filter to the qdisc identified by
502 .BR 999:0.
503 It's priority is
504 .BR 99 ,
505 which affects in which order multiple filters attached to the same
506 .B parent
507 are consulted (the lower the earlier). The filter handles packets of
508 .B protocol
509 type
510 .BR ip ,
511 and
512 .BR match es
513 if the IP header's source address is within the
514 .B 192.168.8.0/24
515 subnet. Matching packets are classified into class
516 .BR 1.1 .
517 The effect of this command might be surprising at first glance:
518
519 .RS
520 .EX
521 filter parent 1: protocol ip pref 99 u32
522 filter parent 1: protocol ip pref 99 u32 \\
523 fh 800: ht divisor 1
524 filter parent 1: protocol ip pref 99 u32 \\
525 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 \\
526 match c0a80800/ffffff00 at 12
527 .EE
528 .RE
529
530 So parent
531 .B 1:
532 is assigned a new
533 .B u32
534 filter, which contains a hash table of size 1 (as the
535 .B divisor
536 indicates). The table ID is
537 .BR 800 .
538 The third line then shows the actual filter which was added above: it sits in
539 table
540 .B 800
541 and bucket
542 .BR 0 ,
543 classifies packets into class ID
544 .B 1:1
545 and matches the upper three bytes of the four byte value at offset
546 .B 12
547 to be
548 .BR 0xc0a808 ,
549 which is 192, 168 and 8.
550
551 Now for something more complicated, namely creating a custom hash table:
552
553 .RS
554 .EX
555 tc filter add dev eth0 prio 99 handle 1: u32 divisor 256
556 .EE
557 .RE
558
559 This creates a table of size 256 with handle
560 .B 1:
561 in priority
562 .BR 99 .
563 The effect is as follows:
564
565 .RS
566 .EX
567 filter parent 1: protocol all pref 99 u32
568 filter parent 1: protocol all pref 99 u32 fh 1: ht divisor 256
569 filter parent 1: protocol all pref 99 u32 fh 800: ht divisor 1
570 .EE
571 .RE
572
573 So along with the requested hash table (handle
574 .BR 1: ),
575 the kernel has created his own table of size 1 to hold other filters of the same
576 priority.
577
578 The next step is to create a filter which links to the created hash table:
579
580 .RS
581 .EX
582 tc filter add dev eth0 parent 1: prio 1 u32 \\
583 link 1: hashkey mask 0x0000ff00 at 12 \\
584 match ip src 192.168.0.0/16
585 .EE
586 .RE
587
588 The filter is given a lower priority than the hash table itself so
589 .B u32
590 consults it before manually traversing the hash table. The options
591 .BR link " and " hashkey
592 determine which table and bucket to redirect to. In this case the hash key
593 should be constructed out of the second byte at offset 12, which corresponds to
594 an IP packet's third byte of the source address field. Along with the
595 .B match
596 statement, this effectively maps all class C networks below 192.168.0.0/16 to
597 different buckets of the hash table.
598
599 Filters for certain subnets can be created like so:
600
601 .RS
602 .EX
603 tc filter add dev eth0 parent 1: prio 99 u32 \\
604 ht 1: sample u32 0x00000800 0x0000ff00 at 12 \\
605 match ip src 192.168.8.0/24 classid 1:1
606 .EE
607 .RE
608
609 The bucket is defined using the
610 .B sample
611 option: In this case, the second byte at offset 12 must be 0x08, exactly. In
612 this case, the resulting bucket ID is obviously 8, but as soon as
613 .B sample
614 selects an amount of data which could exceed the
615 .BR divisor ,
616 one would have to know the kernel-internal algorithm to deduce the destination
617 bucket. This filter's
618 .B match
619 statement is redundant in this case, as the entropy for the hash key does not
620 exceed the table size and therefore no collisions can occur. Otherwise it's
621 necessary to prevent matching unwanted packets.
622
623 Matching upper layer fields is problematic since IPv4 header length is variable
624 and IPv6 supports extension headers which affect upper layer header offset. To
625 overcome this, there is the possibility to specify
626 .B nexthdr+
627 when giving an offset, and to make things easier there are the
628 .BR tcp " and " udp
629 matches which use
630 .B nexthdr+
631 implicitly. This offset has to be calculated in beforehand though, and the only
632 way to achieve that is by doing it in a separate filter which then links to the
633 filter which wants to use it. Here is an example of doing so:
634
635 .RS
636 .EX
637 tc filter add dev eth0 parent 1:0 protocol ip handle 1: \\
638 u32 divisor 1
639 tc filter add dev eth0 parent 1:0 protocol ip \\
640 u32 ht 1: \\
641 match tcp src 22 FFFF \\
642 classid 1:2
643 tc filter add dev eth0 parent 1:0 protocol ip \\
644 u32 ht 800: \\
645 match ip protocol 6 FF \\
646 match ip firstfrag \\
647 offset at 0 mask 0f00 shift 6 \\
648 link 1:
649 .EE
650 .RE
651
652 This is what is being done: In the first call, a single element sized hash table
653 is created so there is a place to hold the linked to filter and a known handle
654 .RB ( 1: )
655 to reference to it. The second call then adds the actual filter, which pushes
656 packets with TCP source port 22 into class
657 .BR 1:2 .
658 Using
659 .BR ht ,
660 it is moved into the hash table created by the first call. The third call then
661 does the actual magic: It matches IPv4 packets with next layer protocol 6 (TCP),
662 only if it's the first fragment (usually TCP sets DF bit, but if it doesn't and
663 the packet is fragmented, only the first one contains the TCP header), and then
664 sets the offset based on the IP header's IHL field (right-shifting by 6
665 eliminates the offset of the field and at the same time converts the value into
666 byte unit). Finally, using
667 .BR link ,
668 the hash table from first call is referenced which holds the filter from second
669 call.
670 .SH SEE ALSO
671 .BR tc (8),
672 .br
673 .BR cls_u32.txt " at " http://linux-tc-notes.sourceforge.net/