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