]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/conntrack.c
2 * Copyright (c) 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "conntrack.h"
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
26 #include "conntrack-private.h"
30 #include "dp-packet.h"
33 #include "odp-netlink.h"
34 #include "openvswitch/hmap.h"
35 #include "openvswitch/vlog.h"
37 #include "ovs-thread.h"
38 #include "poll-loop.h"
42 VLOG_DEFINE_THIS_MODULE(conntrack
);
44 COVERAGE_DEFINE(conntrack_full
);
45 COVERAGE_DEFINE(conntrack_long_cleanup
);
47 struct conn_lookup_ctx
{
55 static bool conn_key_extract(struct conntrack
*, struct dp_packet
*,
56 ovs_be16 dl_type
, struct conn_lookup_ctx
*,
58 static uint32_t conn_key_hash(const struct conn_key
*, uint32_t basis
);
59 static void conn_key_reverse(struct conn_key
*);
60 static void conn_key_lookup(struct conntrack_bucket
*ctb
,
61 struct conn_lookup_ctx
*ctx
,
63 static bool valid_new(struct dp_packet
*pkt
, struct conn_key
*);
64 static struct conn
*new_conn(struct conntrack_bucket
*, struct dp_packet
*pkt
,
65 struct conn_key
*, long long now
);
66 static void delete_conn(struct conn
*);
67 static enum ct_update_res
conn_update(struct conn
*,
68 struct conntrack_bucket
*ctb
,
69 struct dp_packet
*, bool reply
,
71 static bool conn_expired(struct conn
*, long long now
);
72 static void set_mark(struct dp_packet
*, struct conn
*,
73 uint32_t val
, uint32_t mask
);
74 static void set_label(struct dp_packet
*, struct conn
*,
75 const struct ovs_key_ct_labels
*val
,
76 const struct ovs_key_ct_labels
*mask
);
77 static void *clean_thread_main(void *f_
);
79 static struct ct_l4_proto
*l4_protos
[] = {
80 [IPPROTO_TCP
] = &ct_proto_tcp
,
81 [IPPROTO_UDP
] = &ct_proto_other
,
82 [IPPROTO_ICMP
] = &ct_proto_icmp4
,
83 [IPPROTO_ICMPV6
] = &ct_proto_icmp6
,
86 long long ct_timeout_val
[] = {
87 #define CT_TIMEOUT(NAME, VAL) [CT_TM_##NAME] = VAL,
92 /* If the total number of connections goes above this value, no new connections
94 #define DEFAULT_N_CONN_LIMIT 3000000
96 /* Initializes the connection tracker 'ct'. The caller is responsible for
97 * calling 'conntrack_destroy()', when the instance is not needed anymore */
99 conntrack_init(struct conntrack
*ct
)
102 long long now
= time_msec();
104 for (i
= 0; i
< CONNTRACK_BUCKETS
; i
++) {
105 struct conntrack_bucket
*ctb
= &ct
->buckets
[i
];
107 ct_lock_init(&ctb
->lock
);
108 ct_lock_lock(&ctb
->lock
);
109 hmap_init(&ctb
->connections
);
110 for (j
= 0; j
< ARRAY_SIZE(ctb
->exp_lists
); j
++) {
111 ovs_list_init(&ctb
->exp_lists
[j
]);
113 ct_lock_unlock(&ctb
->lock
);
114 ovs_mutex_init(&ctb
->cleanup_mutex
);
115 ovs_mutex_lock(&ctb
->cleanup_mutex
);
116 ctb
->next_cleanup
= now
+ CT_TM_MIN
;
117 ovs_mutex_unlock(&ctb
->cleanup_mutex
);
119 ct
->hash_basis
= random_uint32();
120 atomic_count_init(&ct
->n_conn
, 0);
121 atomic_init(&ct
->n_conn_limit
, DEFAULT_N_CONN_LIMIT
);
122 latch_init(&ct
->clean_thread_exit
);
123 ct
->clean_thread
= ovs_thread_create("ct_clean", clean_thread_main
, ct
);
126 /* Destroys the connection tracker 'ct' and frees all the allocated memory. */
128 conntrack_destroy(struct conntrack
*ct
)
132 latch_set(&ct
->clean_thread_exit
);
133 pthread_join(ct
->clean_thread
, NULL
);
134 latch_destroy(&ct
->clean_thread_exit
);
135 for (i
= 0; i
< CONNTRACK_BUCKETS
; i
++) {
136 struct conntrack_bucket
*ctb
= &ct
->buckets
[i
];
139 ovs_mutex_destroy(&ctb
->cleanup_mutex
);
140 ct_lock_lock(&ctb
->lock
);
141 HMAP_FOR_EACH_POP(conn
, node
, &ctb
->connections
) {
142 atomic_count_dec(&ct
->n_conn
);
145 hmap_destroy(&ctb
->connections
);
146 ct_lock_unlock(&ctb
->lock
);
147 ct_lock_destroy(&ctb
->lock
);
151 static unsigned hash_to_bucket(uint32_t hash
)
153 /* Extracts the most significant bits in hash. The least significant bits
154 * are already used internally by the hmap implementation. */
155 BUILD_ASSERT(CONNTRACK_BUCKETS_SHIFT
< 32 && CONNTRACK_BUCKETS_SHIFT
>= 1);
157 return (hash
>> (32 - CONNTRACK_BUCKETS_SHIFT
)) % CONNTRACK_BUCKETS
;
161 write_ct_md(struct dp_packet
*pkt
, uint16_t state
, uint16_t zone
,
162 uint32_t mark
, ovs_u128 label
)
164 pkt
->md
.ct_state
= state
| CS_TRACKED
;
165 pkt
->md
.ct_zone
= zone
;
166 pkt
->md
.ct_mark
= mark
;
167 pkt
->md
.ct_label
= label
;
171 conn_not_found(struct conntrack
*ct
, struct dp_packet
*pkt
,
172 struct conn_lookup_ctx
*ctx
, uint16_t *state
, bool commit
,
175 unsigned bucket
= hash_to_bucket(ctx
->hash
);
176 struct conn
*nc
= NULL
;
178 if (!valid_new(pkt
, &ctx
->key
)) {
179 *state
|= CS_INVALID
;
186 unsigned int n_conn_limit
;
188 atomic_read_relaxed(&ct
->n_conn_limit
, &n_conn_limit
);
190 if (atomic_count_get(&ct
->n_conn
) >= n_conn_limit
) {
191 COVERAGE_INC(conntrack_full
);
195 nc
= new_conn(&ct
->buckets
[bucket
], pkt
, &ctx
->key
, now
);
197 memcpy(&nc
->rev_key
, &ctx
->key
, sizeof nc
->rev_key
);
199 conn_key_reverse(&nc
->rev_key
);
200 hmap_insert(&ct
->buckets
[bucket
].connections
, &nc
->node
, ctx
->hash
);
201 atomic_count_inc(&ct
->n_conn
);
208 process_one(struct conntrack
*ct
, struct dp_packet
*pkt
,
209 struct conn_lookup_ctx
*ctx
, uint16_t zone
,
210 bool commit
, long long now
)
212 unsigned bucket
= hash_to_bucket(ctx
->hash
);
213 struct conn
*conn
= ctx
->conn
;
220 state
|= CS_REPLY_DIR
;
223 enum ct_update_res res
;
225 res
= conn_update(conn
, &ct
->buckets
[bucket
], pkt
,
229 case CT_UPDATE_VALID
:
230 state
|= CS_ESTABLISHED
;
232 state
|= CS_REPLY_DIR
;
235 case CT_UPDATE_INVALID
:
239 ovs_list_remove(&conn
->exp_node
);
240 hmap_remove(&ct
->buckets
[bucket
].connections
, &conn
->node
);
241 atomic_count_dec(&ct
->n_conn
);
243 conn
= conn_not_found(ct
, pkt
, ctx
, &state
, commit
, now
);
253 conn
= conn_not_found(ct
, pkt
, ctx
, &state
, commit
, now
);
257 write_ct_md(pkt
, state
, zone
, conn
? conn
->mark
: 0,
258 conn
? conn
->label
: OVS_U128_ZERO
);
263 /* Sends the packets in '*pkt_batch' through the connection tracker 'ct'. All
264 * the packets should have the same 'dl_type' (IPv4 or IPv6) and should have
265 * the l3 and and l4 offset properly set.
267 * If 'commit' is true, the packets are allowed to create new entries in the
268 * connection tables. 'setmark', if not NULL, should point to a two
269 * elements array containing a value and a mask to set the connection mark.
270 * 'setlabel' behaves similarly for the connection label.*/
272 conntrack_execute(struct conntrack
*ct
, struct dp_packet_batch
*pkt_batch
,
273 ovs_be16 dl_type
, bool commit
, uint16_t zone
,
274 const uint32_t *setmark
,
275 const struct ovs_key_ct_labels
*setlabel
,
278 struct dp_packet
**pkts
= pkt_batch
->packets
;
279 size_t cnt
= pkt_batch
->count
;
280 #if !defined(__CHECKER__) && !defined(_WIN32)
281 const size_t KEY_ARRAY_SIZE
= cnt
;
283 enum { KEY_ARRAY_SIZE
= NETDEV_MAX_BURST
};
285 struct conn_lookup_ctx ctxs
[KEY_ARRAY_SIZE
];
286 int8_t bucket_list
[CONNTRACK_BUCKETS
];
290 } arr
[KEY_ARRAY_SIZE
];
291 long long now
= time_msec();
295 BUILD_ASSERT_DECL(sizeof arr
[0].maps
* CHAR_BIT
>= NETDEV_MAX_BURST
);
298 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
300 VLOG_WARN_RL(&rl
, "ALG helper \"%s\" not supported", helper
);
301 /* Continue without the helper */
304 memset(bucket_list
, INT8_C(-1), sizeof bucket_list
);
305 for (i
= 0; i
< cnt
; i
++) {
308 if (!conn_key_extract(ct
, pkts
[i
], dl_type
, &ctxs
[i
], zone
)) {
309 write_ct_md(pkts
[i
], CS_INVALID
, zone
, 0, OVS_U128_ZERO
);
313 bucket
= hash_to_bucket(ctxs
[i
].hash
);
314 if (bucket_list
[bucket
] == INT8_C(-1)) {
315 bucket_list
[bucket
] = arrcnt
;
317 arr
[arrcnt
].maps
= 0;
318 ULLONG_SET1(arr
[arrcnt
].maps
, i
);
319 arr
[arrcnt
++].bucket
= bucket
;
321 ULLONG_SET1(arr
[bucket_list
[bucket
]].maps
, i
);
325 for (i
= 0; i
< arrcnt
; i
++) {
326 struct conntrack_bucket
*ctb
= &ct
->buckets
[arr
[i
].bucket
];
329 ct_lock_lock(&ctb
->lock
);
331 ULLONG_FOR_EACH_1(j
, arr
[i
].maps
) {
334 conn_key_lookup(ctb
, &ctxs
[j
], now
);
336 conn
= process_one(ct
, pkts
[j
], &ctxs
[j
], zone
, commit
, now
);
338 if (conn
&& setmark
) {
339 set_mark(pkts
[j
], conn
, setmark
[0], setmark
[1]);
342 if (conn
&& setlabel
) {
343 set_label(pkts
[j
], conn
, &setlabel
[0], &setlabel
[1]);
346 ct_lock_unlock(&ctb
->lock
);
353 set_mark(struct dp_packet
*pkt
, struct conn
*conn
, uint32_t val
, uint32_t mask
)
355 pkt
->md
.ct_mark
= val
| (pkt
->md
.ct_mark
& ~(mask
));
356 conn
->mark
= pkt
->md
.ct_mark
;
360 set_label(struct dp_packet
*pkt
, struct conn
*conn
,
361 const struct ovs_key_ct_labels
*val
,
362 const struct ovs_key_ct_labels
*mask
)
366 memcpy(&v
, val
, sizeof v
);
367 memcpy(&m
, mask
, sizeof m
);
369 pkt
->md
.ct_label
.u64
.lo
= v
.u64
.lo
370 | (pkt
->md
.ct_label
.u64
.lo
& ~(m
.u64
.lo
));
371 pkt
->md
.ct_label
.u64
.hi
= v
.u64
.hi
372 | (pkt
->md
.ct_label
.u64
.hi
& ~(m
.u64
.hi
));
373 conn
->label
= pkt
->md
.ct_label
;
376 /* Delete the expired connections from 'ctb', up to 'limit'. Returns the
377 * earliest expiration time among the remaining connections in 'ctb'. Returns
378 * LLONG_MAX if 'ctb' is empty. The return value might be smaller than 'now',
379 * if 'limit' is reached */
381 sweep_bucket(struct conntrack
*ct
, struct conntrack_bucket
*ctb
, long long now
,
383 OVS_REQUIRES(ctb
->lock
)
385 struct conn
*conn
, *next
;
386 long long min_expiration
= LLONG_MAX
;
390 for (i
= 0; i
< N_CT_TM
; i
++) {
391 LIST_FOR_EACH_SAFE (conn
, next
, exp_node
, &ctb
->exp_lists
[i
]) {
392 if (!conn_expired(conn
, now
) || count
>= limit
) {
393 min_expiration
= MIN(min_expiration
, conn
->expiration
);
394 if (count
>= limit
) {
395 /* Do not check other lists. */
396 COVERAGE_INC(conntrack_long_cleanup
);
397 return min_expiration
;
401 ovs_list_remove(&conn
->exp_node
);
402 hmap_remove(&ctb
->connections
, &conn
->node
);
403 atomic_count_dec(&ct
->n_conn
);
409 return min_expiration
;
412 /* Cleans up old connection entries from 'ct'. Returns the time when the
413 * next expiration might happen. The return value might be smaller than
414 * 'now', meaning that an internal limit has been reached, and some expired
415 * connections have not been deleted. */
417 conntrack_clean(struct conntrack
*ct
, long long now
)
419 long long next_wakeup
= now
+ CT_TM_MIN
;
420 unsigned int n_conn_limit
;
421 size_t clean_count
= 0;
424 atomic_read_relaxed(&ct
->n_conn_limit
, &n_conn_limit
);
426 for (i
= 0; i
< CONNTRACK_BUCKETS
; i
++) {
427 struct conntrack_bucket
*ctb
= &ct
->buckets
[i
];
431 ovs_mutex_lock(&ctb
->cleanup_mutex
);
432 if (ctb
->next_cleanup
> now
) {
436 ct_lock_lock(&ctb
->lock
);
437 prev_count
= hmap_count(&ctb
->connections
);
438 /* If the connections are well distributed among buckets, we want to
439 * limit to 10% of the global limit equally split among buckets. If
440 * the bucket is busier than the others, we limit to 10% of its
442 min_exp
= sweep_bucket(ct
, ctb
, now
,
443 MAX(prev_count
/10, n_conn_limit
/(CONNTRACK_BUCKETS
*10)));
444 clean_count
+= prev_count
- hmap_count(&ctb
->connections
);
447 /* We call hmap_shrink() only if sweep_bucket() managed to delete
448 * every expired connection. */
449 hmap_shrink(&ctb
->connections
);
452 ct_lock_unlock(&ctb
->lock
);
454 ctb
->next_cleanup
= MIN(min_exp
, now
+ CT_TM_MIN
);
457 next_wakeup
= MIN(next_wakeup
, ctb
->next_cleanup
);
458 ovs_mutex_unlock(&ctb
->cleanup_mutex
);
461 VLOG_DBG("conntrack cleanup %"PRIuSIZE
" entries in %lld msec",
462 clean_count
, time_msec() - now
);
469 * We must call conntrack_clean() periodically. conntrack_clean() return
470 * value gives an hint on when the next cleanup must be done (either because
471 * there is an actual connection that expires, or because a new connection
472 * might be created with the minimum timeout).
474 * The logic below has two goals:
476 * - We want to reduce the number of wakeups and batch connection cleanup
477 * when the load is not very high. CT_CLEAN_INTERVAL ensures that if we
478 * are coping with the current cleanup tasks, then we wait at least
479 * 5 seconds to do further cleanup.
481 * - We don't want to keep the buckets locked too long, as we might prevent
482 * traffic from flowing. CT_CLEAN_MIN_INTERVAL ensures that if cleanup is
483 * behind, there is at least some 200ms blocks of time when buckets will be
484 * left alone, so the datapath can operate unhindered.
486 #define CT_CLEAN_INTERVAL 5000 /* 5 seconds */
487 #define CT_CLEAN_MIN_INTERVAL 200 /* 0.2 seconds */
490 clean_thread_main(void *f_
)
492 struct conntrack
*ct
= f_
;
494 while (!latch_is_set(&ct
->clean_thread_exit
)) {
496 long long now
= time_msec();
498 next_wake
= conntrack_clean(ct
, now
);
500 if (next_wake
< now
) {
501 poll_timer_wait_until(now
+ CT_CLEAN_MIN_INTERVAL
);
503 poll_timer_wait_until(MAX(next_wake
, now
+ CT_CLEAN_INTERVAL
));
505 latch_wait(&ct
->clean_thread_exit
);
514 /* The function stores a pointer to the first byte after the header in
515 * '*new_data', if 'new_data' is not NULL. If it is NULL, the caller is
516 * not interested in the header's tail, meaning that the header has
517 * already been parsed (e.g. by flow_extract): we take this as a hint to
518 * save a few checks. If 'validate_checksum' is true, the function returns
519 * false if the IPv4 checksum is invalid. */
521 extract_l3_ipv4(struct conn_key
*key
, const void *data
, size_t size
,
522 const char **new_data
, bool validate_checksum
)
524 const struct ip_header
*ip
= data
;
528 if (OVS_UNLIKELY(size
< IP_HEADER_LEN
)) {
533 ip_len
= IP_IHL(ip
->ip_ihl_ver
) * 4;
536 if (OVS_UNLIKELY(ip_len
< IP_HEADER_LEN
)) {
539 if (OVS_UNLIKELY(size
< ip_len
)) {
543 *new_data
= (char *) data
+ ip_len
;
546 if (IP_IS_FRAGMENT(ip
->ip_frag_off
)) {
550 if (validate_checksum
&& csum(data
, ip_len
) != 0) {
554 key
->src
.addr
.ipv4
= ip
->ip_src
;
555 key
->dst
.addr
.ipv4
= ip
->ip_dst
;
556 key
->nw_proto
= ip
->ip_proto
;
561 /* The function stores a pointer to the first byte after the header in
562 * '*new_data', if 'new_data' is not NULL. If it is NULL, the caller is
563 * not interested in the header's tail, meaning that the header has
564 * already been parsed (e.g. by flow_extract): we take this as a hint to
565 * save a few checks. */
567 extract_l3_ipv6(struct conn_key
*key
, const void *data
, size_t size
,
568 const char **new_data
)
570 const struct ovs_16aligned_ip6_hdr
*ip6
= data
;
571 uint8_t nw_proto
= ip6
->ip6_nxt
;
575 if (OVS_UNLIKELY(size
< sizeof *ip6
)) {
583 if (!parse_ipv6_ext_hdrs(&data
, &size
, &nw_proto
, &nw_frag
)) {
595 key
->src
.addr
.ipv6
= ip6
->ip6_src
;
596 key
->dst
.addr
.ipv6
= ip6
->ip6_dst
;
597 key
->nw_proto
= nw_proto
;
603 checksum_valid(const struct conn_key
*key
, const void *data
, size_t size
,
608 if (key
->dl_type
== htons(ETH_TYPE_IP
)) {
609 csum
= packet_csum_pseudoheader(l3
);
610 } else if (key
->dl_type
== htons(ETH_TYPE_IPV6
)) {
611 csum
= packet_csum_pseudoheader6(l3
);
616 csum
= csum_continue(csum
, data
, size
);
618 return csum_finish(csum
) == 0;
622 check_l4_tcp(const struct conn_key
*key
, const void *data
, size_t size
,
625 const struct tcp_header
*tcp
= data
;
626 size_t tcp_len
= TCP_OFFSET(tcp
->tcp_ctl
) * 4;
628 if (OVS_UNLIKELY(tcp_len
< TCP_HEADER_LEN
|| tcp_len
> size
)) {
632 return checksum_valid(key
, data
, size
, l3
);
636 check_l4_udp(const struct conn_key
*key
, const void *data
, size_t size
,
639 const struct udp_header
*udp
= data
;
640 size_t udp_len
= ntohs(udp
->udp_len
);
642 if (OVS_UNLIKELY(udp_len
< UDP_HEADER_LEN
|| udp_len
> size
)) {
646 /* Validation must be skipped if checksum is 0 on IPv4 packets */
647 return (udp
->udp_csum
== 0 && key
->dl_type
== htons(ETH_TYPE_IP
))
648 || checksum_valid(key
, data
, size
, l3
);
652 check_l4_icmp(const void *data
, size_t size
)
654 return csum(data
, size
) == 0;
658 check_l4_icmp6(const struct conn_key
*key
, const void *data
, size_t size
,
661 return checksum_valid(key
, data
, size
, l3
);
665 extract_l4_tcp(struct conn_key
*key
, const void *data
, size_t size
)
667 const struct tcp_header
*tcp
= data
;
669 if (OVS_UNLIKELY(size
< TCP_HEADER_LEN
)) {
673 key
->src
.port
= tcp
->tcp_src
;
674 key
->dst
.port
= tcp
->tcp_dst
;
676 /* Port 0 is invalid */
677 return key
->src
.port
&& key
->dst
.port
;
681 extract_l4_udp(struct conn_key
*key
, const void *data
, size_t size
)
683 const struct udp_header
*udp
= data
;
685 if (OVS_UNLIKELY(size
< UDP_HEADER_LEN
)) {
689 key
->src
.port
= udp
->udp_src
;
690 key
->dst
.port
= udp
->udp_dst
;
692 /* Port 0 is invalid */
693 return key
->src
.port
&& key
->dst
.port
;
696 static inline bool extract_l4(struct conn_key
*key
, const void *data
,
697 size_t size
, bool *related
, const void *l3
);
700 reverse_icmp_type(uint8_t type
)
703 case ICMP4_ECHO_REQUEST
:
704 return ICMP4_ECHO_REPLY
;
705 case ICMP4_ECHO_REPLY
:
706 return ICMP4_ECHO_REQUEST
;
708 case ICMP4_TIMESTAMP
:
709 return ICMP4_TIMESTAMPREPLY
;
710 case ICMP4_TIMESTAMPREPLY
:
711 return ICMP4_TIMESTAMP
;
713 case ICMP4_INFOREQUEST
:
714 return ICMP4_INFOREPLY
;
715 case ICMP4_INFOREPLY
:
716 return ICMP4_INFOREQUEST
;
722 /* If 'related' is not NULL and the function is processing an ICMP
723 * error packet, extract the l3 and l4 fields from the nested header
724 * instead and set *related to true. If 'related' is NULL we're
725 * already processing a nested header and no such recursion is
728 extract_l4_icmp(struct conn_key
*key
, const void *data
, size_t size
,
731 const struct icmp_header
*icmp
= data
;
733 if (OVS_UNLIKELY(size
< ICMP_HEADER_LEN
)) {
737 switch (icmp
->icmp_type
) {
738 case ICMP4_ECHO_REQUEST
:
739 case ICMP4_ECHO_REPLY
:
740 case ICMP4_TIMESTAMP
:
741 case ICMP4_TIMESTAMPREPLY
:
742 case ICMP4_INFOREQUEST
:
743 case ICMP4_INFOREPLY
:
744 if (icmp
->icmp_code
!= 0) {
747 /* Separate ICMP connection: identified using id */
748 key
->src
.icmp_id
= key
->dst
.icmp_id
= icmp
->icmp_fields
.echo
.id
;
749 key
->src
.icmp_type
= icmp
->icmp_type
;
750 key
->dst
.icmp_type
= reverse_icmp_type(icmp
->icmp_type
);
752 case ICMP4_DST_UNREACH
:
753 case ICMP4_TIME_EXCEEDED
:
754 case ICMP4_PARAM_PROB
:
755 case ICMP4_SOURCEQUENCH
:
756 case ICMP4_REDIRECT
: {
757 /* ICMP packet part of another connection. We should
758 * extract the key from embedded packet header */
759 struct conn_key inner_key
;
760 const char *l3
= (const char *) (icmp
+ 1);
761 const char *tail
= (const char *) data
+ size
;
769 memset(&inner_key
, 0, sizeof inner_key
);
770 inner_key
.dl_type
= htons(ETH_TYPE_IP
);
771 ok
= extract_l3_ipv4(&inner_key
, l3
, tail
- l3
, &l4
, false);
776 /* pf doesn't do this, but it seems a good idea */
777 if (inner_key
.src
.addr
.ipv4_aligned
!= key
->dst
.addr
.ipv4_aligned
778 || inner_key
.dst
.addr
.ipv4_aligned
!= key
->src
.addr
.ipv4_aligned
) {
782 key
->src
= inner_key
.src
;
783 key
->dst
= inner_key
.dst
;
784 key
->nw_proto
= inner_key
.nw_proto
;
786 ok
= extract_l4(key
, l4
, tail
- l4
, NULL
, l3
);
788 conn_key_reverse(key
);
801 reverse_icmp6_type(uint8_t type
)
804 case ICMP6_ECHO_REQUEST
:
805 return ICMP6_ECHO_REPLY
;
806 case ICMP6_ECHO_REPLY
:
807 return ICMP6_ECHO_REQUEST
;
813 /* If 'related' is not NULL and the function is processing an ICMP
814 * error packet, extract the l3 and l4 fields from the nested header
815 * instead and set *related to true. If 'related' is NULL we're
816 * already processing a nested header and no such recursion is
819 extract_l4_icmp6(struct conn_key
*key
, const void *data
, size_t size
,
822 const struct icmp6_header
*icmp6
= data
;
824 /* All the messages that we support need at least 4 bytes after
826 if (size
< sizeof *icmp6
+ 4) {
830 switch (icmp6
->icmp6_type
) {
831 case ICMP6_ECHO_REQUEST
:
832 case ICMP6_ECHO_REPLY
:
833 if (icmp6
->icmp6_code
!= 0) {
836 /* Separate ICMP connection: identified using id */
837 key
->src
.icmp_id
= key
->dst
.icmp_id
= *(ovs_be16
*) (icmp6
+ 1);
838 key
->src
.icmp_type
= icmp6
->icmp6_type
;
839 key
->dst
.icmp_type
= reverse_icmp6_type(icmp6
->icmp6_type
);
841 case ICMP6_DST_UNREACH
:
842 case ICMP6_PACKET_TOO_BIG
:
843 case ICMP6_TIME_EXCEEDED
:
844 case ICMP6_PARAM_PROB
: {
845 /* ICMP packet part of another connection. We should
846 * extract the key from embedded packet header */
847 struct conn_key inner_key
;
848 const char *l3
= (const char *) icmp6
+ 8;
849 const char *tail
= (const char *) data
+ size
;
850 const char *l4
= NULL
;
857 memset(&inner_key
, 0, sizeof inner_key
);
858 inner_key
.dl_type
= htons(ETH_TYPE_IPV6
);
859 ok
= extract_l3_ipv6(&inner_key
, l3
, tail
- l3
, &l4
);
864 /* pf doesn't do this, but it seems a good idea */
865 if (!ipv6_addr_equals(&inner_key
.src
.addr
.ipv6_aligned
,
866 &key
->dst
.addr
.ipv6_aligned
)
867 || !ipv6_addr_equals(&inner_key
.dst
.addr
.ipv6_aligned
,
868 &key
->src
.addr
.ipv6_aligned
)) {
872 key
->src
= inner_key
.src
;
873 key
->dst
= inner_key
.dst
;
874 key
->nw_proto
= inner_key
.nw_proto
;
876 ok
= extract_l4(key
, l4
, tail
- l4
, NULL
, l3
);
878 conn_key_reverse(key
);
890 /* Extract l4 fields into 'key', which must already contain valid l3
893 * If 'related' is not NULL and an ICMP error packet is being
894 * processed, the function will extract the key from the packet nested
895 * in the ICMP paylod and set '*related' to true.
897 * If 'related' is NULL, it means that we're already parsing a header nested
898 * in an ICMP error. In this case, we skip checksum and length validation. */
900 extract_l4(struct conn_key
*key
, const void *data
, size_t size
, bool *related
,
903 if (key
->nw_proto
== IPPROTO_TCP
) {
904 return (!related
|| check_l4_tcp(key
, data
, size
, l3
))
905 && extract_l4_tcp(key
, data
, size
);
906 } else if (key
->nw_proto
== IPPROTO_UDP
) {
907 return (!related
|| check_l4_udp(key
, data
, size
, l3
))
908 && extract_l4_udp(key
, data
, size
);
909 } else if (key
->dl_type
== htons(ETH_TYPE_IP
)
910 && key
->nw_proto
== IPPROTO_ICMP
) {
911 return (!related
|| check_l4_icmp(data
, size
))
912 && extract_l4_icmp(key
, data
, size
, related
);
913 } else if (key
->dl_type
== htons(ETH_TYPE_IPV6
)
914 && key
->nw_proto
== IPPROTO_ICMPV6
) {
915 return (!related
|| check_l4_icmp6(key
, data
, size
, l3
))
916 && extract_l4_icmp6(key
, data
, size
, related
);
923 conn_key_extract(struct conntrack
*ct
, struct dp_packet
*pkt
, ovs_be16 dl_type
,
924 struct conn_lookup_ctx
*ctx
, uint16_t zone
)
926 const struct eth_header
*l2
= dp_packet_l2(pkt
);
927 const struct ip_header
*l3
= dp_packet_l3(pkt
);
928 const char *l4
= dp_packet_l4(pkt
);
929 const char *tail
= dp_packet_tail(pkt
);
932 memset(ctx
, 0, sizeof *ctx
);
934 if (!l2
|| !l3
|| !l4
) {
938 ctx
->key
.zone
= zone
;
940 /* XXX In this function we parse the packet (again, it has already
941 * gone through miniflow_extract()) for two reasons:
943 * 1) To extract the l3 addresses and l4 ports.
944 * We already have the l3 and l4 headers' pointers. Extracting
945 * the l3 addresses and the l4 ports is really cheap, since they
946 * can be found at fixed locations.
947 * 2) To extract the l4 type.
948 * Extracting the l4 types, for IPv6 can be quite expensive, because
949 * it's not at a fixed location.
951 * Here's a way to avoid (2) with the help of the datapath.
952 * The datapath doesn't keep the packet's extracted flow[1], so
953 * using that is not an option. We could use the packet's matching
954 * megaflow, but we have to make sure that the l4 type (nw_proto)
955 * is unwildcarded. This means either:
957 * a) dpif-netdev unwildcards the l4 type when a new flow is installed
958 * if the actions contains ct().
960 * b) ofproto-dpif-xlate unwildcards the l4 type when translating a ct()
961 * action. This is already done in different actions, but it's
962 * unnecessary for the kernel.
965 * [1] The reasons for this are that keeping the flow increases
966 * (slightly) the cache footprint and increases computation
967 * time as we move the packet around. Most importantly, the flow
968 * should be updated by the actions and this can be slow, as
969 * we use a sparse representation (miniflow).
972 ctx
->key
.dl_type
= dl_type
;
973 if (ctx
->key
.dl_type
== htons(ETH_TYPE_IP
)) {
974 ok
= extract_l3_ipv4(&ctx
->key
, l3
, tail
- (char *) l3
, NULL
, true);
975 } else if (ctx
->key
.dl_type
== htons(ETH_TYPE_IPV6
)) {
976 ok
= extract_l3_ipv6(&ctx
->key
, l3
, tail
- (char *) l3
, NULL
);
982 if (extract_l4(&ctx
->key
, l4
, tail
- l4
, &ctx
->related
, l3
)) {
983 ctx
->hash
= conn_key_hash(&ctx
->key
, ct
->hash_basis
);
993 conn_key_hash(const struct conn_key
*key
, uint32_t basis
)
995 uint32_t hsrc
, hdst
, hash
;
1000 /* Hash the source and destination tuple */
1001 for (i
= 0; i
< sizeof(key
->src
) / sizeof(uint32_t); i
++) {
1002 hsrc
= hash_add(hsrc
, ((uint32_t *) &key
->src
)[i
]);
1003 hdst
= hash_add(hdst
, ((uint32_t *) &key
->dst
)[i
]);
1006 /* Even if source and destination are swapped the hash will be the same. */
1009 /* Hash the rest of the key(L3 and L4 types and zone). */
1010 hash
= hash_words((uint32_t *) (&key
->dst
+ 1),
1011 (uint32_t *) (key
+ 1) - (uint32_t *) (&key
->dst
+ 1),
1018 conn_key_reverse(struct conn_key
*key
)
1020 struct ct_endpoint tmp
;
1023 key
->src
= key
->dst
;
1028 conn_key_lookup(struct conntrack_bucket
*ctb
,
1029 struct conn_lookup_ctx
*ctx
,
1032 uint32_t hash
= ctx
->hash
;
1037 HMAP_FOR_EACH_WITH_HASH (conn
, node
, hash
, &ctb
->connections
) {
1038 if (!memcmp(&conn
->key
, &ctx
->key
, sizeof(conn
->key
))
1039 && !conn_expired(conn
, now
)) {
1044 if (!memcmp(&conn
->rev_key
, &ctx
->key
, sizeof(conn
->rev_key
))
1045 && !conn_expired(conn
, now
)) {
1053 static enum ct_update_res
1054 conn_update(struct conn
*conn
, struct conntrack_bucket
*ctb
,
1055 struct dp_packet
*pkt
, bool reply
, long long now
)
1057 return l4_protos
[conn
->key
.nw_proto
]->conn_update(conn
, ctb
, pkt
,
1062 conn_expired(struct conn
*conn
, long long now
)
1064 return now
>= conn
->expiration
;
1068 valid_new(struct dp_packet
*pkt
, struct conn_key
*key
)
1070 return l4_protos
[key
->nw_proto
]->valid_new(pkt
);
1073 static struct conn
*
1074 new_conn(struct conntrack_bucket
*ctb
, struct dp_packet
*pkt
,
1075 struct conn_key
*key
, long long now
)
1077 struct conn
*newconn
;
1079 newconn
= l4_protos
[key
->nw_proto
]->new_conn(ctb
, pkt
, now
);
1082 newconn
->key
= *key
;
1089 delete_conn(struct conn
*conn
)
1095 ct_endpoint_to_ct_dpif_inet_addr(const struct ct_addr
*a
,
1096 union ct_dpif_inet_addr
*b
,
1099 if (dl_type
== htons(ETH_TYPE_IP
)) {
1100 b
->ip
= a
->ipv4_aligned
;
1101 } else if (dl_type
== htons(ETH_TYPE_IPV6
)){
1102 b
->in6
= a
->ipv6_aligned
;
1107 conn_key_to_tuple(const struct conn_key
*key
, struct ct_dpif_tuple
*tuple
)
1109 if (key
->dl_type
== htons(ETH_TYPE_IP
)) {
1110 tuple
->l3_type
= AF_INET
;
1111 } else if (key
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1112 tuple
->l3_type
= AF_INET6
;
1114 tuple
->ip_proto
= key
->nw_proto
;
1115 ct_endpoint_to_ct_dpif_inet_addr(&key
->src
.addr
, &tuple
->src
,
1117 ct_endpoint_to_ct_dpif_inet_addr(&key
->dst
.addr
, &tuple
->dst
,
1120 if (key
->nw_proto
== IPPROTO_ICMP
|| key
->nw_proto
== IPPROTO_ICMPV6
) {
1121 tuple
->icmp_id
= key
->src
.icmp_id
;
1122 tuple
->icmp_type
= key
->src
.icmp_type
;
1123 tuple
->icmp_code
= key
->src
.icmp_code
;
1125 tuple
->src_port
= key
->src
.port
;
1126 tuple
->dst_port
= key
->dst
.port
;
1131 conn_to_ct_dpif_entry(const struct conn
*conn
, struct ct_dpif_entry
*entry
,
1134 struct ct_l4_proto
*class;
1135 long long expiration
;
1136 memset(entry
, 0, sizeof *entry
);
1137 conn_key_to_tuple(&conn
->key
, &entry
->tuple_orig
);
1138 conn_key_to_tuple(&conn
->rev_key
, &entry
->tuple_reply
);
1140 entry
->zone
= conn
->key
.zone
;
1141 entry
->mark
= conn
->mark
;
1143 memcpy(&entry
->labels
, &conn
->label
, sizeof(entry
->labels
));
1144 /* Not implemented yet */
1145 entry
->timestamp
.start
= 0;
1146 entry
->timestamp
.stop
= 0;
1148 expiration
= conn
->expiration
- now
;
1149 entry
->timeout
= (expiration
> 0) ? expiration
/ 1000 : 0;
1151 class = l4_protos
[conn
->key
.nw_proto
];
1152 if (class->conn_get_protoinfo
) {
1153 class->conn_get_protoinfo(conn
, &entry
->protoinfo
);
1158 conntrack_dump_start(struct conntrack
*ct
, struct conntrack_dump
*dump
,
1159 const uint16_t *pzone
)
1161 memset(dump
, 0, sizeof(*dump
));
1163 dump
->zone
= *pzone
;
1164 dump
->filter_zone
= true;
1172 conntrack_dump_next(struct conntrack_dump
*dump
, struct ct_dpif_entry
*entry
)
1174 struct conntrack
*ct
= dump
->ct
;
1175 long long now
= time_msec();
1177 while (dump
->bucket
< CONNTRACK_BUCKETS
) {
1178 struct hmap_node
*node
;
1180 ct_lock_lock(&ct
->buckets
[dump
->bucket
].lock
);
1184 node
= hmap_at_position(&ct
->buckets
[dump
->bucket
].connections
,
1189 INIT_CONTAINER(conn
, node
, node
);
1190 if (!dump
->filter_zone
|| conn
->key
.zone
== dump
->zone
) {
1191 conn_to_ct_dpif_entry(conn
, entry
, now
);
1194 /* Else continue, until we find an entry in the appropriate zone
1195 * or the bucket has been scanned completely. */
1197 ct_lock_unlock(&ct
->buckets
[dump
->bucket
].lock
);
1200 memset(&dump
->bucket_pos
, 0, sizeof dump
->bucket_pos
);
1210 conntrack_dump_done(struct conntrack_dump
*dump OVS_UNUSED
)
1216 conntrack_flush(struct conntrack
*ct
, const uint16_t *zone
)
1220 for (i
= 0; i
< CONNTRACK_BUCKETS
; i
++) {
1221 struct conn
*conn
, *next
;
1223 ct_lock_lock(&ct
->buckets
[i
].lock
);
1224 HMAP_FOR_EACH_SAFE(conn
, next
, node
, &ct
->buckets
[i
].connections
) {
1225 if (!zone
|| *zone
== conn
->key
.zone
) {
1226 ovs_list_remove(&conn
->exp_node
);
1227 hmap_remove(&ct
->buckets
[i
].connections
, &conn
->node
);
1228 atomic_count_dec(&ct
->n_conn
);
1232 ct_lock_unlock(&ct
->buckets
[i
].lock
);