netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / ofproto / tunnel.c
1 /* Copyright (c) 2013, 2014, 2015 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14
15 #include <config.h>
16 #include "tunnel.h"
17
18 #include <errno.h>
19
20 #include "byte-order.h"
21 #include "connectivity.h"
22 #include "csum.h"
23 #include "dpif.h"
24 #include "dynamic-string.h"
25 #include "fat-rwlock.h"
26 #include "hash.h"
27 #include "hmap.h"
28 #include "netdev.h"
29 #include "odp-util.h"
30 #include "ofpbuf.h"
31 #include "packets.h"
32 #include "route-table.h"
33 #include "seq.h"
34 #include "smap.h"
35 #include "socket-util.h"
36 #include "tnl-ports.h"
37 #include "tunnel.h"
38 #include "openvswitch/vlog.h"
39 #include "unaligned.h"
40 #include "ofproto-dpif.h"
41
42 VLOG_DEFINE_THIS_MODULE(tunnel);
43
44 /* skb mark used for IPsec tunnel packets */
45 #define IPSEC_MARK 1
46
47 struct tnl_match {
48     ovs_be64 in_key;
49     struct in6_addr ipv6_src;
50     struct in6_addr ipv6_dst;
51     odp_port_t odp_port;
52     uint32_t pkt_mark;
53     bool in_key_flow;
54     bool ip_src_flow;
55     bool ip_dst_flow;
56 };
57
58 struct tnl_port {
59     struct hmap_node ofport_node;
60     struct hmap_node match_node;
61
62     const struct ofport_dpif *ofport;
63     uint64_t change_seq;
64     struct netdev *netdev;
65
66     struct tnl_match match;
67 };
68
69 static struct fat_rwlock rwlock;
70
71 /* Tunnel matches.
72  *
73  * This module maps packets received over tunnel protocols to vports.  The
74  * tunnel protocol and, for some protocols, tunnel-specific information (e.g.,
75  * for VXLAN, the UDP destination port number) are always use as part of the
76  * mapping.  Which other fields are used for the mapping depends on the vports
77  * themselves (the parenthesized notations refer to "struct tnl_match" fields):
78  *
79  *     - in_key: A vport may match a specific tunnel ID (in_key_flow == false)
80  *       or arrange for the tunnel ID to be matched as tunnel.tun_id in the
81  *       OpenFlow flow (in_key_flow == true).
82  *
83  *     - ip_dst: A vport may match a specific destination IP address
84  *       (ip_dst_flow == false) or arrange for the destination IP to be matched
85  *       as tunnel.ip_dst in the OpenFlow flow (ip_dst_flow == true).
86  *
87  *     - ip_src: A vport may match a specific IP source address (ip_src_flow ==
88  *       false, ip_src != 0), wildcard all source addresses (ip_src_flow ==
89  *       false, ip_src == 0), or arrange for the IP source address to be
90  *       handled in the OpenFlow flow table (ip_src_flow == true).
91  *
92  * Thus, there are 2 * 2 * 3 == 12 possible ways a vport can match against a
93  * tunnel packet.  We number the possibilities for each field in increasing
94  * order as listed in each bullet above.  We order the 12 overall combinations
95  * in lexicographic order considering in_key first, then ip_dst, then
96  * ip_src. */
97 #define N_MATCH_TYPES (2 * 2 * 3)
98
99 /* The three possibilities (see above) for vport ip_src matches. */
100 enum ip_src_type {
101     IP_SRC_CFG,             /* ip_src must equal configured address. */
102     IP_SRC_ANY,             /* Any ip_src is acceptable. */
103     IP_SRC_FLOW             /* ip_src is handled in flow table. */
104 };
105
106 /* Each hmap contains "struct tnl_port"s.
107  * The index is a combination of how each of the fields listed under "Tunnel
108  * matches" above matches, see the final paragraph for ordering. */
109 static struct hmap *tnl_match_maps[N_MATCH_TYPES] OVS_GUARDED_BY(rwlock);
110 static struct hmap **tnl_match_map(const struct tnl_match *);
111
112 static struct hmap ofport_map__ = HMAP_INITIALIZER(&ofport_map__);
113 static struct hmap *ofport_map OVS_GUARDED_BY(rwlock) = &ofport_map__;
114
115 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
116 static struct vlog_rate_limit dbg_rl = VLOG_RATE_LIMIT_INIT(60, 60);
117
118 static struct tnl_port *tnl_find(const struct flow *) OVS_REQ_RDLOCK(rwlock);
119 static struct tnl_port *tnl_find_exact(struct tnl_match *, struct hmap *)
120     OVS_REQ_RDLOCK(rwlock);
121 static struct tnl_port *tnl_find_ofport(const struct ofport_dpif *)
122     OVS_REQ_RDLOCK(rwlock);
123
124 static uint32_t tnl_hash(struct tnl_match *);
125 static void tnl_match_fmt(const struct tnl_match *, struct ds *);
126 static char *tnl_port_fmt(const struct tnl_port *) OVS_REQ_RDLOCK(rwlock);
127 static void tnl_port_mod_log(const struct tnl_port *, const char *action)
128     OVS_REQ_RDLOCK(rwlock);
129 static const char *tnl_port_get_name(const struct tnl_port *)
130     OVS_REQ_RDLOCK(rwlock);
131 static void tnl_port_del__(const struct ofport_dpif *) OVS_REQ_WRLOCK(rwlock);
132
133 void
134 ofproto_tunnel_init(void)
135 {
136     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
137
138     if (ovsthread_once_start(&once)) {
139         fat_rwlock_init(&rwlock);
140         ovsthread_once_done(&once);
141     }
142 }
143
144 static bool
145 tnl_port_add__(const struct ofport_dpif *ofport, const struct netdev *netdev,
146                odp_port_t odp_port, bool warn, bool native_tnl, const char name[])
147     OVS_REQ_WRLOCK(rwlock)
148 {
149     const struct netdev_tunnel_config *cfg;
150     struct tnl_port *existing_port;
151     struct tnl_port *tnl_port;
152     struct hmap **map;
153
154     cfg = netdev_get_tunnel_config(netdev);
155     ovs_assert(cfg);
156
157     tnl_port = xzalloc(sizeof *tnl_port);
158     tnl_port->ofport = ofport;
159     tnl_port->netdev = netdev_ref(netdev);
160     tnl_port->change_seq = netdev_get_change_seq(tnl_port->netdev);
161
162     tnl_port->match.in_key = cfg->in_key;
163     tnl_port->match.ipv6_src = cfg->ipv6_src;
164     tnl_port->match.ipv6_dst = cfg->ipv6_dst;
165     tnl_port->match.ip_src_flow = cfg->ip_src_flow;
166     tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
167     tnl_port->match.pkt_mark = cfg->ipsec ? IPSEC_MARK : 0;
168     tnl_port->match.in_key_flow = cfg->in_key_flow;
169     tnl_port->match.odp_port = odp_port;
170
171     map = tnl_match_map(&tnl_port->match);
172     existing_port = tnl_find_exact(&tnl_port->match, *map);
173     if (existing_port) {
174         if (warn) {
175             struct ds ds = DS_EMPTY_INITIALIZER;
176             tnl_match_fmt(&tnl_port->match, &ds);
177             VLOG_WARN("%s: attempting to add tunnel port with same config as "
178                       "port '%s' (%s)", tnl_port_get_name(tnl_port),
179                       tnl_port_get_name(existing_port), ds_cstr(&ds));
180             ds_destroy(&ds);
181         }
182         netdev_close(tnl_port->netdev);
183         free(tnl_port);
184         return false;
185     }
186
187     hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
188
189     if (!*map) {
190         *map = xmalloc(sizeof **map);
191         hmap_init(*map);
192     }
193     hmap_insert(*map, &tnl_port->match_node, tnl_hash(&tnl_port->match));
194     tnl_port_mod_log(tnl_port, "adding");
195
196     if (native_tnl) {
197         tnl_port_map_insert(odp_port, cfg->dst_port, name);
198     }
199     return true;
200 }
201
202 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
203  * must be added before they can be used by the module. 'ofport' must be a
204  * tunnel.
205  *
206  * Returns 0 if successful, otherwise a positive errno value. */
207 int
208 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
209              odp_port_t odp_port, bool native_tnl, const char name[]) OVS_EXCLUDED(rwlock)
210 {
211     bool ok;
212
213     fat_rwlock_wrlock(&rwlock);
214     ok = tnl_port_add__(ofport, netdev, odp_port, true, native_tnl, name);
215     fat_rwlock_unlock(&rwlock);
216
217     return ok ? 0 : EEXIST;
218 }
219
220 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
221  * in its netdev_tunnel_config.  If it does, returns true. Otherwise, returns
222  * false.  'ofport' and 'odp_port' should be the same as would be passed to
223  * tnl_port_add(). */
224 bool
225 tnl_port_reconfigure(const struct ofport_dpif *ofport,
226                      const struct netdev *netdev, odp_port_t odp_port,
227                      bool native_tnl, const char name[])
228     OVS_EXCLUDED(rwlock)
229 {
230     struct tnl_port *tnl_port;
231     bool changed = false;
232
233     fat_rwlock_wrlock(&rwlock);
234     tnl_port = tnl_find_ofport(ofport);
235     if (!tnl_port) {
236         changed = tnl_port_add__(ofport, netdev, odp_port, false, native_tnl, name);
237     } else if (tnl_port->netdev != netdev
238                || tnl_port->match.odp_port != odp_port
239                || tnl_port->change_seq != netdev_get_change_seq(tnl_port->netdev)) {
240         VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
241         tnl_port_del__(ofport);
242         tnl_port_add__(ofport, netdev, odp_port, true, native_tnl, name);
243         changed = true;
244     }
245     fat_rwlock_unlock(&rwlock);
246     return changed;
247 }
248
249 static void
250 tnl_port_del__(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(rwlock)
251 {
252     struct tnl_port *tnl_port;
253
254     if (!ofport) {
255         return;
256     }
257
258     tnl_port = tnl_find_ofport(ofport);
259     if (tnl_port) {
260         const struct netdev_tunnel_config *cfg =
261             netdev_get_tunnel_config(tnl_port->netdev);
262         struct hmap **map;
263
264         tnl_port_map_delete(cfg->dst_port);
265         tnl_port_mod_log(tnl_port, "removing");
266         map = tnl_match_map(&tnl_port->match);
267         hmap_remove(*map, &tnl_port->match_node);
268         if (hmap_is_empty(*map)) {
269             hmap_destroy(*map);
270             free(*map);
271             *map = NULL;
272         }
273         hmap_remove(ofport_map, &tnl_port->ofport_node);
274         netdev_close(tnl_port->netdev);
275         free(tnl_port);
276     }
277 }
278
279 /* Removes 'ofport' from the module. */
280 void
281 tnl_port_del(const struct ofport_dpif *ofport) OVS_EXCLUDED(rwlock)
282 {
283     fat_rwlock_wrlock(&rwlock);
284     tnl_port_del__(ofport);
285     fat_rwlock_unlock(&rwlock);
286 }
287
288 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
289  * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
290  * none is found.
291  *
292  * Callers should verify that 'flow' needs to be received by calling
293  * tnl_port_should_receive() before this function. */
294 const struct ofport_dpif *
295 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
296 {
297     char *pre_flow_str = NULL;
298     const struct ofport_dpif *ofport;
299     struct tnl_port *tnl_port;
300
301     fat_rwlock_rdlock(&rwlock);
302     tnl_port = tnl_find(flow);
303     ofport = tnl_port ? tnl_port->ofport : NULL;
304     if (!tnl_port) {
305         char *flow_str = flow_to_string(flow);
306
307         VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", flow_str);
308         free(flow_str);
309         goto out;
310     }
311
312     if (!VLOG_DROP_DBG(&dbg_rl)) {
313         pre_flow_str = flow_to_string(flow);
314     }
315
316     if (pre_flow_str) {
317         char *post_flow_str = flow_to_string(flow);
318         char *tnl_str = tnl_port_fmt(tnl_port);
319         VLOG_DBG("flow received\n"
320                  "%s"
321                  " pre: %s\n"
322                  "post: %s",
323                  tnl_str, pre_flow_str, post_flow_str);
324         free(tnl_str);
325         free(pre_flow_str);
326         free(post_flow_str);
327     }
328
329 out:
330     fat_rwlock_unlock(&rwlock);
331     return ofport;
332 }
333
334 /* Should be called at the beginning of action translation to initialize
335  * wildcards and perform any actions based on receiving on tunnel port.
336  *
337  * Returns false if the packet must be dropped. */
338 bool
339 tnl_process_ecn(struct flow *flow)
340 {
341     if (!tnl_port_should_receive(flow)) {
342         return true;
343     }
344
345     if (is_ip_any(flow) && (flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
346         if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
347             VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
348                          " but is not ECN capable");
349             return false;
350         }
351
352         /* Set the ECN CE value in the tunneled packet. */
353         flow->nw_tos |= IP_ECN_CE;
354     }
355
356     flow->pkt_mark &= ~IPSEC_MARK;
357     return true;
358 }
359
360 void
361 tnl_wc_init(struct flow *flow, struct flow_wildcards *wc)
362 {
363     if (tnl_port_should_receive(flow)) {
364         wc->masks.tunnel.tun_id = OVS_BE64_MAX;
365         if (flow->tunnel.ip_dst) {
366             wc->masks.tunnel.ip_src = OVS_BE32_MAX;
367             wc->masks.tunnel.ip_dst = OVS_BE32_MAX;
368         } else {
369             wc->masks.tunnel.ipv6_src = in6addr_exact;
370             wc->masks.tunnel.ipv6_dst = in6addr_exact;
371         }
372         wc->masks.tunnel.flags = (FLOW_TNL_F_DONT_FRAGMENT |
373                                   FLOW_TNL_F_CSUM |
374                                   FLOW_TNL_F_KEY);
375         wc->masks.tunnel.ip_tos = UINT8_MAX;
376         wc->masks.tunnel.ip_ttl = 0;
377         /* The tp_src and tp_dst members in flow_tnl are set to be always
378          * wildcarded, not to unwildcard them here. */
379         wc->masks.tunnel.tp_src = 0;
380         wc->masks.tunnel.tp_dst = 0;
381
382         memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
383
384         if (is_ip_any(flow)
385             && (flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
386             wc->masks.nw_tos |= IP_ECN_MASK;
387         }
388     }
389 }
390
391 /* Given that 'flow' should be output to the ofport corresponding to
392  * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
393  * port that the output should happen on.  May return ODPP_NONE if the output
394  * shouldn't occur. */
395 odp_port_t
396 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
397               struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
398 {
399     const struct netdev_tunnel_config *cfg;
400     struct tnl_port *tnl_port;
401     char *pre_flow_str = NULL;
402     odp_port_t out_port;
403
404     fat_rwlock_rdlock(&rwlock);
405     tnl_port = tnl_find_ofport(ofport);
406     out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
407     if (!tnl_port) {
408         goto out;
409     }
410
411     cfg = netdev_get_tunnel_config(tnl_port->netdev);
412     ovs_assert(cfg);
413
414     if (!VLOG_DROP_DBG(&dbg_rl)) {
415         pre_flow_str = flow_to_string(flow);
416     }
417
418     if (!cfg->ip_src_flow) {
419         flow->tunnel.ip_src = in6_addr_get_mapped_ipv4(&tnl_port->match.ipv6_src);
420         if (!flow->tunnel.ip_src) {
421             flow->tunnel.ipv6_src = tnl_port->match.ipv6_src;
422         }
423     }
424     if (!cfg->ip_dst_flow) {
425         flow->tunnel.ip_dst = in6_addr_get_mapped_ipv4(&tnl_port->match.ipv6_dst);
426         if (!flow->tunnel.ip_dst) {
427             flow->tunnel.ipv6_dst = tnl_port->match.ipv6_dst;
428         }
429     }
430     if (ipv6_addr_is_set(&flow->tunnel.ipv6_dst) ||
431         ipv6_addr_is_set(&flow->tunnel.ipv6_src)) {
432         out_port = ODPP_NONE;
433         VLOG_WARN_RL(&rl, "port (%s): IPv6 tunnel endpoint is not supported",
434                      netdev_get_name(tnl_port->netdev));
435         goto out;
436     }
437
438     flow->pkt_mark = tnl_port->match.pkt_mark;
439
440     if (!cfg->out_key_flow) {
441         flow->tunnel.tun_id = cfg->out_key;
442     }
443
444     if (cfg->ttl_inherit && is_ip_any(flow)) {
445         wc->masks.nw_ttl = 0xff;
446         flow->tunnel.ip_ttl = flow->nw_ttl;
447     } else {
448         flow->tunnel.ip_ttl = cfg->ttl;
449     }
450
451     if (cfg->tos_inherit && is_ip_any(flow)) {
452         wc->masks.nw_tos |= IP_DSCP_MASK;
453         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
454     } else {
455         flow->tunnel.ip_tos = cfg->tos;
456     }
457
458     /* ECN fields are always inherited. */
459     if (is_ip_any(flow)) {
460         wc->masks.nw_tos |= IP_ECN_MASK;
461
462         if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
463             flow->tunnel.ip_tos |= IP_ECN_ECT_0;
464         } else {
465             flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
466         }
467     }
468
469     flow->tunnel.flags |= (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
470         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
471         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
472
473     if (pre_flow_str) {
474         char *post_flow_str = flow_to_string(flow);
475         char *tnl_str = tnl_port_fmt(tnl_port);
476         VLOG_DBG("flow sent\n"
477                  "%s"
478                  " pre: %s\n"
479                  "post: %s",
480                  tnl_str, pre_flow_str, post_flow_str);
481         free(tnl_str);
482         free(pre_flow_str);
483         free(post_flow_str);
484     }
485
486 out:
487     fat_rwlock_unlock(&rwlock);
488     return out_port;
489 }
490
491 static uint32_t
492 tnl_hash(struct tnl_match *match)
493 {
494     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
495     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
496 }
497
498 static struct tnl_port *
499 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
500 {
501     struct tnl_port *tnl_port;
502
503     HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
504                              ofport_map) {
505         if (tnl_port->ofport == ofport) {
506             return tnl_port;
507         }
508     }
509     return NULL;
510 }
511
512 static struct tnl_port *
513 tnl_find_exact(struct tnl_match *match, struct hmap *map)
514     OVS_REQ_RDLOCK(rwlock)
515 {
516     if (map) {
517         struct tnl_port *tnl_port;
518
519         HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match), map) {
520             if (!memcmp(match, &tnl_port->match, sizeof *match)) {
521                 return tnl_port;
522             }
523         }
524     }
525     return NULL;
526 }
527
528 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
529  * or NULL if no tnl_port matches 'flow'. */
530 static struct tnl_port *
531 tnl_find(const struct flow *flow) OVS_REQ_RDLOCK(rwlock)
532 {
533     enum ip_src_type ip_src;
534     int in_key_flow;
535     int ip_dst_flow;
536     int i;
537
538     i = 0;
539     for (in_key_flow = 0; in_key_flow < 2; in_key_flow++) {
540         for (ip_dst_flow = 0; ip_dst_flow < 2; ip_dst_flow++) {
541             for (ip_src = 0; ip_src < 3; ip_src++) {
542                 struct hmap *map = tnl_match_maps[i];
543
544                 if (map) {
545                     struct tnl_port *tnl_port;
546                     struct tnl_match match;
547
548                     memset(&match, 0, sizeof match);
549
550                     /* The apparent mix-up of 'ip_dst' and 'ip_src' below is
551                      * correct, because "struct tnl_match" is expressed in
552                      * terms of packets being sent out, but we are using it
553                      * here as a description of how to treat received
554                      * packets. */
555                     match.in_key = in_key_flow ? 0 : flow->tunnel.tun_id;
556                     if (ip_src == IP_SRC_CFG) {
557                         match.ipv6_src = flow_tnl_dst(&flow->tunnel);
558                     }
559                     if (!ip_dst_flow) {
560                         match.ipv6_dst = flow_tnl_src(&flow->tunnel);
561                     }
562                     match.odp_port = flow->in_port.odp_port;
563                     match.pkt_mark = flow->pkt_mark;
564                     match.in_key_flow = in_key_flow;
565                     match.ip_dst_flow = ip_dst_flow;
566                     match.ip_src_flow = ip_src == IP_SRC_FLOW;
567
568                     tnl_port = tnl_find_exact(&match, map);
569                     if (tnl_port) {
570                         return tnl_port;
571                     }
572                 }
573
574                 i++;
575             }
576         }
577     }
578
579     return NULL;
580 }
581
582 /* Returns a pointer to the 'tnl_match_maps' element corresponding to 'm''s
583  * matching criteria. */
584 static struct hmap **
585 tnl_match_map(const struct tnl_match *m)
586 {
587     enum ip_src_type ip_src;
588
589     ip_src = (m->ip_src_flow ? IP_SRC_FLOW
590               : ipv6_addr_is_set(&m->ipv6_src) ? IP_SRC_CFG
591               : IP_SRC_ANY);
592
593     return &tnl_match_maps[6 * m->in_key_flow + 3 * m->ip_dst_flow + ip_src];
594 }
595
596 static void
597 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
598     OVS_REQ_RDLOCK(rwlock)
599 {
600     if (!match->ip_dst_flow) {
601         ipv6_format_mapped(&match->ipv6_src, ds);
602         ds_put_cstr(ds, "->");
603         ipv6_format_mapped(&match->ipv6_dst, ds);
604     } else if (!match->ip_src_flow) {
605         ipv6_format_mapped(&match->ipv6_src, ds);
606         ds_put_cstr(ds, "->flow");
607     } else {
608         ds_put_cstr(ds, "flow->flow");
609     }
610
611     if (match->in_key_flow) {
612         ds_put_cstr(ds, ", key=flow");
613     } else {
614         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
615     }
616
617     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
618     ds_put_format(ds, ", pkt mark=%"PRIu32, match->pkt_mark);
619 }
620
621 static void
622 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
623     OVS_REQ_RDLOCK(rwlock)
624 {
625     if (VLOG_IS_DBG_ENABLED()) {
626         struct ds ds = DS_EMPTY_INITIALIZER;
627
628         tnl_match_fmt(&tnl_port->match, &ds);
629         VLOG_INFO("%s tunnel port %s (%s)", action,
630                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
631         ds_destroy(&ds);
632     }
633 }
634
635 static char *
636 tnl_port_fmt(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
637 {
638     const struct netdev_tunnel_config *cfg =
639         netdev_get_tunnel_config(tnl_port->netdev);
640     struct ds ds = DS_EMPTY_INITIALIZER;
641
642     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
643                   tnl_port_get_name(tnl_port),
644                   netdev_get_type(tnl_port->netdev));
645     tnl_match_fmt(&tnl_port->match, &ds);
646
647     if (cfg->out_key != cfg->in_key ||
648         cfg->out_key_present != cfg->in_key_present ||
649         cfg->out_key_flow != cfg->in_key_flow) {
650         ds_put_cstr(&ds, ", out_key=");
651         if (!cfg->out_key_present) {
652             ds_put_cstr(&ds, "none");
653         } else if (cfg->out_key_flow) {
654             ds_put_cstr(&ds, "flow");
655         } else {
656             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
657         }
658     }
659
660     if (cfg->ttl_inherit) {
661         ds_put_cstr(&ds, ", ttl=inherit");
662     } else {
663         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
664     }
665
666     if (cfg->tos_inherit) {
667         ds_put_cstr(&ds, ", tos=inherit");
668     } else if (cfg->tos) {
669         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
670     }
671
672     if (!cfg->dont_fragment) {
673         ds_put_cstr(&ds, ", df=false");
674     }
675
676     if (cfg->csum) {
677         ds_put_cstr(&ds, ", csum=true");
678     }
679
680     ds_put_cstr(&ds, ")\n");
681
682     return ds_steal_cstr(&ds);
683 }
684
685 static const char *
686 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
687 {
688     return netdev_get_name(tnl_port->netdev);
689 }
690
691 int
692 tnl_port_build_header(const struct ofport_dpif *ofport,
693                       const struct flow *tnl_flow,
694                       const struct eth_addr dmac,
695                       const struct eth_addr smac,
696                       const struct in6_addr * ipv6_src,
697                       struct ovs_action_push_tnl *data)
698 {
699     struct tnl_port *tnl_port;
700     struct eth_header *eth;
701     struct ip_header *ip;
702     struct ovs_16aligned_ip6_hdr *ip6;
703     void *l3;
704     int res;
705     ovs_be32 ip_src;
706
707     fat_rwlock_rdlock(&rwlock);
708     tnl_port = tnl_find_ofport(ofport);
709     ovs_assert(tnl_port);
710
711     ip_src = in6_addr_get_mapped_ipv4(ipv6_src);
712
713     /* Build Ethernet and IP headers. */
714     memset(data->header, 0, sizeof data->header);
715
716     eth = (struct eth_header *)data->header;
717     eth->eth_dst = dmac;
718     eth->eth_src = smac;
719     eth->eth_type = ip_src ? htons(ETH_TYPE_IP) : htons(ETH_TYPE_IPV6);
720
721     l3 = (eth + 1);
722
723     if (ip_src) {
724         ip = (struct ip_header *) l3;
725
726         ip->ip_ihl_ver = IP_IHL_VER(5, 4);
727         ip->ip_tos = tnl_flow->tunnel.ip_tos;
728         ip->ip_ttl = tnl_flow->tunnel.ip_ttl;
729         ip->ip_frag_off = (tnl_flow->tunnel.flags & FLOW_TNL_F_DONT_FRAGMENT) ?
730                           htons(IP_DF) : 0;
731
732         put_16aligned_be32(&ip->ip_src, ip_src);
733         put_16aligned_be32(&ip->ip_dst, tnl_flow->tunnel.ip_dst);
734     } else {
735         ip6 = (struct ovs_16aligned_ip6_hdr *) l3;
736
737         ip6->ip6_vfc = 0x60;
738         ip6->ip6_hlim = tnl_flow->tunnel.ip_ttl;
739
740         /* next header, plen - at netdev_build_header? */
741
742         memcpy(&ip6->ip6_src, ipv6_src, sizeof(ovs_be32[4]));
743         memcpy(&ip6->ip6_dst, &tnl_flow->tunnel.ipv6_dst, sizeof(ovs_be32[4]));
744     }
745
746     res = netdev_build_header(tnl_port->netdev, data, tnl_flow);
747
748     if (ip_src) {
749         ip->ip_csum = csum(ip, sizeof *ip);
750     }
751
752     fat_rwlock_unlock(&rwlock);
753
754     return res;
755 }