ovsdb-monitor: allow multiple jsonrpc monitors to share a single ovsdb
[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     ovs_be32 ip_src;
51     ovs_be32 ip_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     tnl_port->match.ip_src = cfg->ip_src;
165     tnl_port->match.ip_dst = cfg->ip_dst;
166     tnl_port->match.ip_src_flow = cfg->ip_src_flow;
167     tnl_port->match.ip_dst_flow = cfg->ip_dst_flow;
168     tnl_port->match.pkt_mark = cfg->ipsec ? IPSEC_MARK : 0;
169     tnl_port->match.in_key_flow = cfg->in_key_flow;
170     tnl_port->match.odp_port = odp_port;
171
172     map = tnl_match_map(&tnl_port->match);
173     existing_port = tnl_find_exact(&tnl_port->match, *map);
174     if (existing_port) {
175         if (warn) {
176             struct ds ds = DS_EMPTY_INITIALIZER;
177             tnl_match_fmt(&tnl_port->match, &ds);
178             VLOG_WARN("%s: attempting to add tunnel port with same config as "
179                       "port '%s' (%s)", tnl_port_get_name(tnl_port),
180                       tnl_port_get_name(existing_port), ds_cstr(&ds));
181             ds_destroy(&ds);
182         }
183         netdev_close(tnl_port->netdev);
184         free(tnl_port);
185         return false;
186     }
187
188     hmap_insert(ofport_map, &tnl_port->ofport_node, hash_pointer(ofport, 0));
189
190     if (!*map) {
191         *map = xmalloc(sizeof **map);
192         hmap_init(*map);
193     }
194     hmap_insert(*map, &tnl_port->match_node, tnl_hash(&tnl_port->match));
195     tnl_port_mod_log(tnl_port, "adding");
196
197     if (native_tnl) {
198         tnl_port_map_insert(odp_port, tnl_port->match.ip_dst,
199                             cfg->dst_port, name);
200     }
201     return true;
202 }
203
204 /* Adds 'ofport' to the module with datapath port number 'odp_port'. 'ofport's
205  * must be added before they can be used by the module. 'ofport' must be a
206  * tunnel.
207  *
208  * Returns 0 if successful, otherwise a positive errno value. */
209 int
210 tnl_port_add(const struct ofport_dpif *ofport, const struct netdev *netdev,
211              odp_port_t odp_port, bool native_tnl, const char name[]) OVS_EXCLUDED(rwlock)
212 {
213     bool ok;
214
215     fat_rwlock_wrlock(&rwlock);
216     ok = tnl_port_add__(ofport, netdev, odp_port, true, native_tnl, name);
217     fat_rwlock_unlock(&rwlock);
218
219     return ok ? 0 : EEXIST;
220 }
221
222 /* Checks if the tunnel represented by 'ofport' reconfiguration due to changes
223  * in its netdev_tunnel_config.  If it does, returns true. Otherwise, returns
224  * false.  'ofport' and 'odp_port' should be the same as would be passed to
225  * tnl_port_add(). */
226 bool
227 tnl_port_reconfigure(const struct ofport_dpif *ofport,
228                      const struct netdev *netdev, odp_port_t odp_port,
229                      bool native_tnl, const char name[])
230     OVS_EXCLUDED(rwlock)
231 {
232     struct tnl_port *tnl_port;
233     bool changed = false;
234
235     fat_rwlock_wrlock(&rwlock);
236     tnl_port = tnl_find_ofport(ofport);
237     if (!tnl_port) {
238         changed = tnl_port_add__(ofport, netdev, odp_port, false, native_tnl, name);
239     } else if (tnl_port->netdev != netdev
240                || tnl_port->match.odp_port != odp_port
241                || tnl_port->change_seq != netdev_get_change_seq(tnl_port->netdev)) {
242         VLOG_DBG("reconfiguring %s", tnl_port_get_name(tnl_port));
243         tnl_port_del__(ofport);
244         tnl_port_add__(ofport, netdev, odp_port, true, native_tnl, name);
245         changed = true;
246     }
247     fat_rwlock_unlock(&rwlock);
248     return changed;
249 }
250
251 static void
252 tnl_port_del__(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(rwlock)
253 {
254     struct tnl_port *tnl_port;
255
256     if (!ofport) {
257         return;
258     }
259
260     tnl_port = tnl_find_ofport(ofport);
261     if (tnl_port) {
262         const struct netdev_tunnel_config *cfg =
263             netdev_get_tunnel_config(tnl_port->netdev);
264         struct hmap **map;
265
266         tnl_port_map_delete(tnl_port->match.ip_dst, cfg->dst_port);
267         tnl_port_mod_log(tnl_port, "removing");
268         map = tnl_match_map(&tnl_port->match);
269         hmap_remove(*map, &tnl_port->match_node);
270         if (hmap_is_empty(*map)) {
271             hmap_destroy(*map);
272             free(*map);
273             *map = NULL;
274         }
275         hmap_remove(ofport_map, &tnl_port->ofport_node);
276         netdev_close(tnl_port->netdev);
277         free(tnl_port);
278     }
279 }
280
281 /* Removes 'ofport' from the module. */
282 void
283 tnl_port_del(const struct ofport_dpif *ofport) OVS_EXCLUDED(rwlock)
284 {
285     fat_rwlock_wrlock(&rwlock);
286     tnl_port_del__(ofport);
287     fat_rwlock_unlock(&rwlock);
288 }
289
290 /* Looks in the table of tunnels for a tunnel matching the metadata in 'flow'.
291  * Returns the 'ofport' corresponding to the new in_port, or a null pointer if
292  * none is found.
293  *
294  * Callers should verify that 'flow' needs to be received by calling
295  * tnl_port_should_receive() before this function. */
296 const struct ofport_dpif *
297 tnl_port_receive(const struct flow *flow) OVS_EXCLUDED(rwlock)
298 {
299     char *pre_flow_str = NULL;
300     const struct ofport_dpif *ofport;
301     struct tnl_port *tnl_port;
302
303     fat_rwlock_rdlock(&rwlock);
304     tnl_port = tnl_find(flow);
305     ofport = tnl_port ? tnl_port->ofport : NULL;
306     if (!tnl_port) {
307         char *flow_str = flow_to_string(flow);
308
309         VLOG_WARN_RL(&rl, "receive tunnel port not found (%s)", flow_str);
310         free(flow_str);
311         goto out;
312     }
313
314     if (!VLOG_DROP_DBG(&dbg_rl)) {
315         pre_flow_str = flow_to_string(flow);
316     }
317
318     if (pre_flow_str) {
319         char *post_flow_str = flow_to_string(flow);
320         char *tnl_str = tnl_port_fmt(tnl_port);
321         VLOG_DBG("flow received\n"
322                  "%s"
323                  " pre: %s\n"
324                  "post: %s",
325                  tnl_str, pre_flow_str, post_flow_str);
326         free(tnl_str);
327         free(pre_flow_str);
328         free(post_flow_str);
329     }
330
331 out:
332     fat_rwlock_unlock(&rwlock);
333     return ofport;
334 }
335
336 static bool
337 tnl_ecn_ok(const struct flow *base_flow, struct flow *flow,
338            struct flow_wildcards *wc)
339 {
340     if (is_ip_any(base_flow)) {
341         if ((flow->tunnel.ip_tos & IP_ECN_MASK) == IP_ECN_CE) {
342             if (wc) {
343                 wc->masks.nw_tos |= IP_ECN_MASK;
344             }
345             if ((base_flow->nw_tos & IP_ECN_MASK) == IP_ECN_NOT_ECT) {
346                 VLOG_WARN_RL(&rl, "dropping tunnel packet marked ECN CE"
347                              " but is not ECN capable");
348                 return false;
349             } else {
350                 /* Set the ECN CE value in the tunneled packet. */
351                 flow->nw_tos |= IP_ECN_CE;
352             }
353         }
354     }
355
356     return true;
357 }
358
359 /* Should be called at the beginning of action translation to initialize
360  * wildcards and perform any actions based on receiving on tunnel port.
361  *
362  * Returns false if the packet must be dropped. */
363 bool
364 tnl_xlate_init(const struct flow *base_flow, struct flow *flow,
365                struct flow_wildcards *wc)
366 {
367     /* tnl_port_should_receive() examines the 'tunnel.ip_dst' field to
368      * determine the presence of the tunnel metadata.  However, since tunnels'
369      * datapath port numbers are different from the non-tunnel ports, and we
370      * always unwildcard the 'in_port', we do not need to unwildcard
371      * the 'tunnel.ip_dst' for non-tunneled packets. */
372     if (tnl_port_should_receive(flow)) {
373         if (wc) {
374             wc->masks.tunnel.tun_id = OVS_BE64_MAX;
375             wc->masks.tunnel.ip_src = OVS_BE32_MAX;
376             wc->masks.tunnel.ip_dst = OVS_BE32_MAX;
377             wc->masks.tunnel.flags = (FLOW_TNL_F_DONT_FRAGMENT |
378                                       FLOW_TNL_F_CSUM |
379                                       FLOW_TNL_F_KEY);
380             wc->masks.tunnel.ip_tos = UINT8_MAX;
381             wc->masks.tunnel.ip_ttl = UINT8_MAX;
382             /* The tp_src and tp_dst members in flow_tnl are set to be always
383              * wildcarded, not to unwildcard them here. */
384             wc->masks.tunnel.tp_src = 0;
385             wc->masks.tunnel.tp_dst = 0;
386
387             memset(&wc->masks.pkt_mark, 0xff, sizeof wc->masks.pkt_mark);
388         }
389         if (!tnl_ecn_ok(base_flow, flow, wc)) {
390             return false;
391         }
392
393         flow->pkt_mark &= ~IPSEC_MARK;
394     }
395
396     return true;
397 }
398
399 /* Given that 'flow' should be output to the ofport corresponding to
400  * 'tnl_port', updates 'flow''s tunnel headers and returns the actual datapath
401  * port that the output should happen on.  May return ODPP_NONE if the output
402  * shouldn't occur. */
403 odp_port_t
404 tnl_port_send(const struct ofport_dpif *ofport, struct flow *flow,
405               struct flow_wildcards *wc) OVS_EXCLUDED(rwlock)
406 {
407     const struct netdev_tunnel_config *cfg;
408     struct tnl_port *tnl_port;
409     char *pre_flow_str = NULL;
410     odp_port_t out_port;
411
412     fat_rwlock_rdlock(&rwlock);
413     tnl_port = tnl_find_ofport(ofport);
414     out_port = tnl_port ? tnl_port->match.odp_port : ODPP_NONE;
415     if (!tnl_port) {
416         goto out;
417     }
418
419     cfg = netdev_get_tunnel_config(tnl_port->netdev);
420     ovs_assert(cfg);
421
422     if (!VLOG_DROP_DBG(&dbg_rl)) {
423         pre_flow_str = flow_to_string(flow);
424     }
425
426     if (!cfg->ip_src_flow) {
427         flow->tunnel.ip_src = tnl_port->match.ip_src;
428     }
429     if (!cfg->ip_dst_flow) {
430         flow->tunnel.ip_dst = tnl_port->match.ip_dst;
431     }
432     flow->pkt_mark = tnl_port->match.pkt_mark;
433
434     if (!cfg->out_key_flow) {
435         flow->tunnel.tun_id = cfg->out_key;
436     }
437
438     if (cfg->ttl_inherit && is_ip_any(flow)) {
439         wc->masks.nw_ttl = 0xff;
440         flow->tunnel.ip_ttl = flow->nw_ttl;
441     } else {
442         flow->tunnel.ip_ttl = cfg->ttl;
443     }
444
445     if (cfg->tos_inherit && is_ip_any(flow)) {
446         wc->masks.nw_tos |= IP_DSCP_MASK;
447         flow->tunnel.ip_tos = flow->nw_tos & IP_DSCP_MASK;
448     } else {
449         flow->tunnel.ip_tos = cfg->tos;
450     }
451
452     /* ECN fields are always inherited. */
453     if (is_ip_any(flow)) {
454         wc->masks.nw_tos |= IP_ECN_MASK;
455
456         if ((flow->nw_tos & IP_ECN_MASK) == IP_ECN_CE) {
457             flow->tunnel.ip_tos |= IP_ECN_ECT_0;
458         } else {
459             flow->tunnel.ip_tos |= flow->nw_tos & IP_ECN_MASK;
460         }
461     }
462
463     flow->tunnel.flags = (cfg->dont_fragment ? FLOW_TNL_F_DONT_FRAGMENT : 0)
464         | (cfg->csum ? FLOW_TNL_F_CSUM : 0)
465         | (cfg->out_key_present ? FLOW_TNL_F_KEY : 0);
466
467     if (pre_flow_str) {
468         char *post_flow_str = flow_to_string(flow);
469         char *tnl_str = tnl_port_fmt(tnl_port);
470         VLOG_DBG("flow sent\n"
471                  "%s"
472                  " pre: %s\n"
473                  "post: %s",
474                  tnl_str, pre_flow_str, post_flow_str);
475         free(tnl_str);
476         free(pre_flow_str);
477         free(post_flow_str);
478     }
479
480 out:
481     fat_rwlock_unlock(&rwlock);
482     return out_port;
483 }
484
485 static uint32_t
486 tnl_hash(struct tnl_match *match)
487 {
488     BUILD_ASSERT_DECL(sizeof *match % sizeof(uint32_t) == 0);
489     return hash_words((uint32_t *) match, sizeof *match / sizeof(uint32_t), 0);
490 }
491
492 static struct tnl_port *
493 tnl_find_ofport(const struct ofport_dpif *ofport) OVS_REQ_RDLOCK(rwlock)
494 {
495     struct tnl_port *tnl_port;
496
497     HMAP_FOR_EACH_IN_BUCKET (tnl_port, ofport_node, hash_pointer(ofport, 0),
498                              ofport_map) {
499         if (tnl_port->ofport == ofport) {
500             return tnl_port;
501         }
502     }
503     return NULL;
504 }
505
506 static struct tnl_port *
507 tnl_find_exact(struct tnl_match *match, struct hmap *map)
508     OVS_REQ_RDLOCK(rwlock)
509 {
510     if (map) {
511         struct tnl_port *tnl_port;
512
513         HMAP_FOR_EACH_WITH_HASH (tnl_port, match_node, tnl_hash(match), map) {
514             if (!memcmp(match, &tnl_port->match, sizeof *match)) {
515                 return tnl_port;
516             }
517         }
518     }
519     return NULL;
520 }
521
522 /* Returns the tnl_port that is the best match for the tunnel data in 'flow',
523  * or NULL if no tnl_port matches 'flow'. */
524 static struct tnl_port *
525 tnl_find(const struct flow *flow) OVS_REQ_RDLOCK(rwlock)
526 {
527     enum ip_src_type ip_src;
528     int in_key_flow;
529     int ip_dst_flow;
530     int i;
531
532     i = 0;
533     for (in_key_flow = 0; in_key_flow < 2; in_key_flow++) {
534         for (ip_dst_flow = 0; ip_dst_flow < 2; ip_dst_flow++) {
535             for (ip_src = 0; ip_src < 3; ip_src++) {
536                 struct hmap *map = tnl_match_maps[i];
537
538                 if (map) {
539                     struct tnl_port *tnl_port;
540                     struct tnl_match match;
541
542                     memset(&match, 0, sizeof match);
543
544                     /* The apparent mix-up of 'ip_dst' and 'ip_src' below is
545                      * correct, because "struct tnl_match" is expressed in
546                      * terms of packets being sent out, but we are using it
547                      * here as a description of how to treat received
548                      * packets. */
549                     match.in_key = in_key_flow ? 0 : flow->tunnel.tun_id;
550                     match.ip_src = (ip_src == IP_SRC_CFG
551                                     ? flow->tunnel.ip_dst
552                                     : 0);
553                     match.ip_dst = ip_dst_flow ? 0 : flow->tunnel.ip_src;
554                     match.odp_port = flow->in_port.odp_port;
555                     match.pkt_mark = flow->pkt_mark;
556                     match.in_key_flow = in_key_flow;
557                     match.ip_dst_flow = ip_dst_flow;
558                     match.ip_src_flow = ip_src == IP_SRC_FLOW;
559
560                     tnl_port = tnl_find_exact(&match, map);
561                     if (tnl_port) {
562                         return tnl_port;
563                     }
564                 }
565
566                 i++;
567             }
568         }
569     }
570
571     return NULL;
572 }
573
574 /* Returns a pointer to the 'tnl_match_maps' element corresponding to 'm''s
575  * matching criteria. */
576 static struct hmap **
577 tnl_match_map(const struct tnl_match *m)
578 {
579     enum ip_src_type ip_src;
580
581     ip_src = (m->ip_src_flow ? IP_SRC_FLOW
582               : m->ip_src ? IP_SRC_CFG
583               : IP_SRC_ANY);
584
585     return &tnl_match_maps[6 * m->in_key_flow + 3 * m->ip_dst_flow + ip_src];
586 }
587
588 static void
589 tnl_match_fmt(const struct tnl_match *match, struct ds *ds)
590     OVS_REQ_RDLOCK(rwlock)
591 {
592     if (!match->ip_dst_flow) {
593         ds_put_format(ds, IP_FMT"->"IP_FMT, IP_ARGS(match->ip_src),
594                       IP_ARGS(match->ip_dst));
595     } else if (!match->ip_src_flow) {
596         ds_put_format(ds, IP_FMT"->flow", IP_ARGS(match->ip_src));
597     } else {
598         ds_put_cstr(ds, "flow->flow");
599     }
600
601     if (match->in_key_flow) {
602         ds_put_cstr(ds, ", key=flow");
603     } else {
604         ds_put_format(ds, ", key=%#"PRIx64, ntohll(match->in_key));
605     }
606
607     ds_put_format(ds, ", dp port=%"PRIu32, match->odp_port);
608     ds_put_format(ds, ", pkt mark=%"PRIu32, match->pkt_mark);
609 }
610
611 static void
612 tnl_port_mod_log(const struct tnl_port *tnl_port, const char *action)
613     OVS_REQ_RDLOCK(rwlock)
614 {
615     if (VLOG_IS_DBG_ENABLED()) {
616         struct ds ds = DS_EMPTY_INITIALIZER;
617
618         tnl_match_fmt(&tnl_port->match, &ds);
619         VLOG_INFO("%s tunnel port %s (%s)", action,
620                   tnl_port_get_name(tnl_port), ds_cstr(&ds));
621         ds_destroy(&ds);
622     }
623 }
624
625 static char *
626 tnl_port_fmt(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
627 {
628     const struct netdev_tunnel_config *cfg =
629         netdev_get_tunnel_config(tnl_port->netdev);
630     struct ds ds = DS_EMPTY_INITIALIZER;
631
632     ds_put_format(&ds, "port %"PRIu32": %s (%s: ", tnl_port->match.odp_port,
633                   tnl_port_get_name(tnl_port),
634                   netdev_get_type(tnl_port->netdev));
635     tnl_match_fmt(&tnl_port->match, &ds);
636
637     if (cfg->out_key != cfg->in_key ||
638         cfg->out_key_present != cfg->in_key_present ||
639         cfg->out_key_flow != cfg->in_key_flow) {
640         ds_put_cstr(&ds, ", out_key=");
641         if (!cfg->out_key_present) {
642             ds_put_cstr(&ds, "none");
643         } else if (cfg->out_key_flow) {
644             ds_put_cstr(&ds, "flow");
645         } else {
646             ds_put_format(&ds, "%#"PRIx64, ntohll(cfg->out_key));
647         }
648     }
649
650     if (cfg->ttl_inherit) {
651         ds_put_cstr(&ds, ", ttl=inherit");
652     } else {
653         ds_put_format(&ds, ", ttl=%"PRIu8, cfg->ttl);
654     }
655
656     if (cfg->tos_inherit) {
657         ds_put_cstr(&ds, ", tos=inherit");
658     } else if (cfg->tos) {
659         ds_put_format(&ds, ", tos=%#"PRIx8, cfg->tos);
660     }
661
662     if (!cfg->dont_fragment) {
663         ds_put_cstr(&ds, ", df=false");
664     }
665
666     if (cfg->csum) {
667         ds_put_cstr(&ds, ", csum=true");
668     }
669
670     ds_put_cstr(&ds, ")\n");
671
672     return ds_steal_cstr(&ds);
673 }
674
675 static const char *
676 tnl_port_get_name(const struct tnl_port *tnl_port) OVS_REQ_RDLOCK(rwlock)
677 {
678     return netdev_get_name(tnl_port->netdev);
679 }
680
681 int
682 tnl_port_build_header(const struct ofport_dpif *ofport,
683                       const struct flow *tnl_flow,
684                       uint8_t dmac[ETH_ADDR_LEN],
685                       uint8_t smac[ETH_ADDR_LEN],
686                       ovs_be32 ip_src, struct ovs_action_push_tnl *data)
687 {
688     struct tnl_port *tnl_port;
689     struct eth_header *eth;
690     struct ip_header *ip;
691     void *l3;
692     int res;
693
694     fat_rwlock_rdlock(&rwlock);
695     tnl_port = tnl_find_ofport(ofport);
696     ovs_assert(tnl_port);
697
698     /* Build Ethernet and IP headers. */
699     memset(data->header, 0, sizeof data->header);
700
701     eth = (struct eth_header *)data->header;
702     memcpy(eth->eth_dst, dmac, ETH_ADDR_LEN);
703     memcpy(eth->eth_src, smac, ETH_ADDR_LEN);
704     eth->eth_type = htons(ETH_TYPE_IP);
705
706     l3 = (eth + 1);
707     ip = (struct ip_header *) l3;
708
709     ip->ip_ihl_ver = IP_IHL_VER(5, 4);
710     ip->ip_tos = tnl_flow->tunnel.ip_tos;
711     ip->ip_ttl = tnl_flow->tunnel.ip_ttl;
712     ip->ip_frag_off = (tnl_flow->tunnel.flags & FLOW_TNL_F_DONT_FRAGMENT) ?
713                       htons(IP_DF) : 0;
714
715     put_16aligned_be32(&ip->ip_src, ip_src);
716     put_16aligned_be32(&ip->ip_dst, tnl_flow->tunnel.ip_dst);
717
718     res = netdev_build_header(tnl_port->netdev, data, tnl_flow);
719     ip->ip_csum = csum(ip, sizeof *ip);
720     fat_rwlock_unlock(&rwlock);
721
722     return res;
723 }