~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to lib/odp-execute.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
 
2
 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3
3
 * Copyright (c) 2013 Simon Horman
4
4
 *
5
5
 * Licensed under the Apache License, Version 2.0 (the "License");
17
17
 
18
18
#include <config.h>
19
19
#include "odp-execute.h"
20
 
#include <linux/openvswitch.h>
 
20
#include <arpa/inet.h>
 
21
#include <netinet/in.h>
 
22
#include <netinet/icmp6.h>
 
23
#include <netinet/ip6.h>
21
24
#include <stdlib.h>
22
25
#include <string.h>
23
26
 
 
27
#include "dp-packet.h"
24
28
#include "dpif.h"
25
29
#include "netlink.h"
26
 
#include "ofpbuf.h"
 
30
#include "odp-netlink.h"
27
31
#include "odp-util.h"
28
32
#include "packets.h"
29
33
#include "flow.h"
30
34
#include "unaligned.h"
31
35
#include "util.h"
32
36
 
33
 
static void
34
 
odp_eth_set_addrs(struct ofpbuf *packet,
35
 
                  const struct ovs_key_ethernet *eth_key)
36
 
{
37
 
    struct eth_header *eh = ofpbuf_l2(packet);
 
37
/* Masked copy of an ethernet address. 'src' is already properly masked. */
 
38
static void
 
39
ether_addr_copy_masked(uint8_t *dst, const uint8_t *src,
 
40
                       const uint8_t *mask)
 
41
{
 
42
    int i;
 
43
 
 
44
    for (i = 0; i < ETH_ADDR_LEN; i++) {
 
45
        dst[i] = src[i] | (dst[i] & ~mask[i]);
 
46
    }
 
47
}
 
48
 
 
49
static void
 
50
odp_eth_set_addrs(struct dp_packet *packet, const struct ovs_key_ethernet *key,
 
51
                  const struct ovs_key_ethernet *mask)
 
52
{
 
53
    struct eth_header *eh = dp_packet_l2(packet);
38
54
 
39
55
    if (eh) {
40
 
        memcpy(eh->eth_src, eth_key->eth_src, sizeof eh->eth_src);
41
 
        memcpy(eh->eth_dst, eth_key->eth_dst, sizeof eh->eth_dst);
 
56
        if (!mask) {
 
57
            memcpy(eh->eth_src, key->eth_src, sizeof eh->eth_src);
 
58
            memcpy(eh->eth_dst, key->eth_dst, sizeof eh->eth_dst);
 
59
        } else {
 
60
            ether_addr_copy_masked(eh->eth_src, key->eth_src, mask->eth_src);
 
61
            ether_addr_copy_masked(eh->eth_dst, key->eth_dst, mask->eth_dst);
 
62
        }
 
63
    }
 
64
}
 
65
 
 
66
static void
 
67
odp_set_ipv4(struct dp_packet *packet, const struct ovs_key_ipv4 *key,
 
68
             const struct ovs_key_ipv4 *mask)
 
69
{
 
70
    struct ip_header *nh = dp_packet_l3(packet);
 
71
 
 
72
    packet_set_ipv4(
 
73
        packet,
 
74
        key->ipv4_src | (get_16aligned_be32(&nh->ip_src) & ~mask->ipv4_src),
 
75
        key->ipv4_dst | (get_16aligned_be32(&nh->ip_dst) & ~mask->ipv4_dst),
 
76
        key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos),
 
77
        key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl));
 
78
}
 
79
 
 
80
static const ovs_be32 *
 
81
mask_ipv6_addr(const ovs_16aligned_be32 *old, const ovs_be32 *addr,
 
82
               const ovs_be32 *mask, ovs_be32 *masked)
 
83
{
 
84
    for (int i = 0; i < 4; i++) {
 
85
        masked[i] = addr[i] | (get_16aligned_be32(&old[i]) & ~mask[i]);
 
86
    }
 
87
 
 
88
    return masked;
 
89
}
 
90
 
 
91
static void
 
92
odp_set_ipv6(struct dp_packet *packet, const struct ovs_key_ipv6 *key,
 
93
             const struct ovs_key_ipv6 *mask)
 
94
{
 
95
    struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet);
 
96
    ovs_be32 sbuf[4], dbuf[4];
 
97
    uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20;
 
98
    ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff);
 
99
 
 
100
    packet_set_ipv6(
 
101
        packet,
 
102
        key->ipv6_proto,
 
103
        mask_ipv6_addr(nh->ip6_src.be32, key->ipv6_src, mask->ipv6_src, sbuf),
 
104
        mask_ipv6_addr(nh->ip6_dst.be32, key->ipv6_dst, mask->ipv6_dst, dbuf),
 
105
        key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass),
 
106
        key->ipv6_label | (old_fl & ~mask->ipv6_label),
 
107
        key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit));
 
108
}
 
109
 
 
110
static void
 
111
odp_set_tcp(struct dp_packet *packet, const struct ovs_key_tcp *key,
 
112
             const struct ovs_key_tcp *mask)
 
113
{
 
114
    struct tcp_header *th = dp_packet_l4(packet);
 
115
 
 
116
    if (OVS_LIKELY(th && dp_packet_get_tcp_payload(packet))) {
 
117
        packet_set_tcp_port(packet,
 
118
                            key->tcp_src | (th->tcp_src & ~mask->tcp_src),
 
119
                            key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst));
 
120
    }
 
121
}
 
122
 
 
123
static void
 
124
odp_set_udp(struct dp_packet *packet, const struct ovs_key_udp *key,
 
125
             const struct ovs_key_udp *mask)
 
126
{
 
127
    struct udp_header *uh = dp_packet_l4(packet);
 
128
 
 
129
    if (OVS_LIKELY(uh && dp_packet_get_udp_payload(packet))) {
 
130
        packet_set_udp_port(packet,
 
131
                            key->udp_src | (uh->udp_src & ~mask->udp_src),
 
132
                            key->udp_dst | (uh->udp_dst & ~mask->udp_dst));
 
133
    }
 
134
}
 
135
 
 
136
static void
 
137
odp_set_sctp(struct dp_packet *packet, const struct ovs_key_sctp *key,
 
138
             const struct ovs_key_sctp *mask)
 
139
{
 
140
    struct sctp_header *sh = dp_packet_l4(packet);
 
141
 
 
142
    if (OVS_LIKELY(sh && dp_packet_get_sctp_payload(packet))) {
 
143
        packet_set_sctp_port(packet,
 
144
                             key->sctp_src | (sh->sctp_src & ~mask->sctp_src),
 
145
                             key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst));
42
146
    }
43
147
}
44
148
 
52
156
}
53
157
 
54
158
static void
55
 
set_arp(struct ofpbuf *packet, const struct ovs_key_arp *arp_key)
56
 
{
57
 
    struct arp_eth_header *arp = ofpbuf_l3(packet);
58
 
 
59
 
    arp->ar_op = arp_key->arp_op;
60
 
    memcpy(arp->ar_sha, arp_key->arp_sha, ETH_ADDR_LEN);
61
 
    put_16aligned_be32(&arp->ar_spa, arp_key->arp_sip);
62
 
    memcpy(arp->ar_tha, arp_key->arp_tha, ETH_ADDR_LEN);
63
 
    put_16aligned_be32(&arp->ar_tpa, arp_key->arp_tip);
64
 
}
65
 
 
66
 
static void
67
 
odp_execute_set_action(struct ofpbuf *packet, const struct nlattr *a,
68
 
                       struct pkt_metadata *md)
 
159
set_arp(struct dp_packet *packet, const struct ovs_key_arp *key,
 
160
        const struct ovs_key_arp *mask)
 
161
{
 
162
    struct arp_eth_header *arp = dp_packet_l3(packet);
 
163
 
 
164
    if (!mask) {
 
165
        arp->ar_op = key->arp_op;
 
166
        memcpy(arp->ar_sha, key->arp_sha, ETH_ADDR_LEN);
 
167
        put_16aligned_be32(&arp->ar_spa, key->arp_sip);
 
168
        memcpy(arp->ar_tha, key->arp_tha, ETH_ADDR_LEN);
 
169
        put_16aligned_be32(&arp->ar_tpa, key->arp_tip);
 
170
    } else {
 
171
        ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa);
 
172
        ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa);
 
173
 
 
174
        arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op);
 
175
        ether_addr_copy_masked(arp->ar_sha, key->arp_sha, mask->arp_sha);
 
176
        put_16aligned_be32(&arp->ar_spa,
 
177
                           key->arp_sip | (ar_spa & ~mask->arp_sip));
 
178
        ether_addr_copy_masked(arp->ar_tha, key->arp_tha, mask->arp_tha);
 
179
        put_16aligned_be32(&arp->ar_tpa,
 
180
                           key->arp_tip | (ar_tpa & ~mask->arp_tip));
 
181
    }
 
182
}
 
183
 
 
184
static void
 
185
odp_set_nd(struct dp_packet *packet, const struct ovs_key_nd *key,
 
186
           const struct ovs_key_nd *mask)
 
187
{
 
188
    const struct ovs_nd_msg *ns = dp_packet_l4(packet);
 
189
    const struct ovs_nd_opt *nd_opt = dp_packet_get_nd_payload(packet);
 
190
 
 
191
    if (OVS_LIKELY(ns && nd_opt)) {
 
192
        int bytes_remain = dp_packet_l4_size(packet) - sizeof(*ns);
 
193
        ovs_be32 tgt_buf[4];
 
194
        uint8_t sll_buf[ETH_ADDR_LEN] = {0};
 
195
        uint8_t tll_buf[ETH_ADDR_LEN] = {0};
 
196
 
 
197
        while (bytes_remain >= ND_OPT_LEN && nd_opt->nd_opt_len != 0) {
 
198
            if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
 
199
                && nd_opt->nd_opt_len == 1) {
 
200
                memcpy(sll_buf, nd_opt->nd_opt_data, ETH_ADDR_LEN);
 
201
                ether_addr_copy_masked(sll_buf, key->nd_sll, mask->nd_sll);
 
202
 
 
203
                /* A packet can only contain one SLL or TLL option */
 
204
                break;
 
205
            } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
 
206
                       && nd_opt->nd_opt_len == 1) {
 
207
                memcpy(tll_buf, nd_opt->nd_opt_data, ETH_ADDR_LEN);
 
208
                ether_addr_copy_masked(tll_buf, key->nd_tll, mask->nd_tll);
 
209
 
 
210
                /* A packet can only contain one SLL or TLL option */
 
211
                break;
 
212
            }
 
213
 
 
214
            nd_opt += nd_opt->nd_opt_len;
 
215
            bytes_remain -= nd_opt->nd_opt_len * ND_OPT_LEN;
 
216
        }
 
217
 
 
218
        packet_set_nd(packet,
 
219
                      mask_ipv6_addr(ns->target.be32,
 
220
                                     key->nd_target, mask->nd_target, tgt_buf),
 
221
                      sll_buf,
 
222
                      tll_buf);
 
223
    }
 
224
}
 
225
 
 
226
static void
 
227
odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a)
69
228
{
70
229
    enum ovs_key_attr type = nl_attr_type(a);
71
230
    const struct ovs_key_ipv4 *ipv4_key;
72
231
    const struct ovs_key_ipv6 *ipv6_key;
73
 
    const struct ovs_key_tcp *tcp_key;
74
 
    const struct ovs_key_udp *udp_key;
75
 
    const struct ovs_key_sctp *sctp_key;
 
232
    struct pkt_metadata *md = &packet->md;
76
233
 
77
234
    switch (type) {
78
235
    case OVS_KEY_ATTR_PRIORITY:
88
245
        break;
89
246
 
90
247
    case OVS_KEY_ATTR_ETHERNET:
91
 
        odp_eth_set_addrs(packet,
92
 
                          nl_attr_get_unspec(a, sizeof(struct ovs_key_ethernet)));
 
248
        odp_eth_set_addrs(packet, nl_attr_get(a), NULL);
93
249
        break;
94
250
 
95
251
    case OVS_KEY_ATTR_IPV4:
96
252
        ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
97
 
        packet_set_ipv4(packet, ipv4_key->ipv4_src, ipv4_key->ipv4_dst,
98
 
                        ipv4_key->ipv4_tos, ipv4_key->ipv4_ttl);
 
253
        packet_set_ipv4(packet, ipv4_key->ipv4_src,
 
254
                        ipv4_key->ipv4_dst, ipv4_key->ipv4_tos,
 
255
                        ipv4_key->ipv4_ttl);
99
256
        break;
100
257
 
101
258
    case OVS_KEY_ATTR_IPV6:
102
259
        ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
103
 
        packet_set_ipv6(packet, ipv6_key->ipv6_proto, ipv6_key->ipv6_src,
104
 
                        ipv6_key->ipv6_dst, ipv6_key->ipv6_tclass,
105
 
                        ipv6_key->ipv6_label, ipv6_key->ipv6_hlimit);
 
260
        packet_set_ipv6(packet, ipv6_key->ipv6_proto,
 
261
                        ipv6_key->ipv6_src, ipv6_key->ipv6_dst,
 
262
                        ipv6_key->ipv6_tclass, ipv6_key->ipv6_label,
 
263
                        ipv6_key->ipv6_hlimit);
106
264
        break;
107
265
 
108
266
    case OVS_KEY_ATTR_TCP:
109
 
        tcp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
110
 
        packet_set_tcp_port(packet, tcp_key->tcp_src, tcp_key->tcp_dst);
 
267
        if (OVS_LIKELY(dp_packet_get_tcp_payload(packet))) {
 
268
            const struct ovs_key_tcp *tcp_key
 
269
                = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
 
270
 
 
271
            packet_set_tcp_port(packet, tcp_key->tcp_src,
 
272
                                tcp_key->tcp_dst);
 
273
        }
111
274
        break;
112
275
 
113
276
    case OVS_KEY_ATTR_UDP:
114
 
        udp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
115
 
        packet_set_udp_port(packet, udp_key->udp_src, udp_key->udp_dst);
 
277
        if (OVS_LIKELY(dp_packet_get_udp_payload(packet))) {
 
278
            const struct ovs_key_udp *udp_key
 
279
                = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
 
280
 
 
281
            packet_set_udp_port(packet, udp_key->udp_src,
 
282
                                udp_key->udp_dst);
 
283
        }
116
284
        break;
117
285
 
118
286
    case OVS_KEY_ATTR_SCTP:
119
 
        sctp_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
120
 
        packet_set_sctp_port(packet, sctp_key->sctp_src, sctp_key->sctp_dst);
 
287
        if (OVS_LIKELY(dp_packet_get_sctp_payload(packet))) {
 
288
            const struct ovs_key_sctp *sctp_key
 
289
                = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
 
290
 
 
291
            packet_set_sctp_port(packet, sctp_key->sctp_src,
 
292
                                 sctp_key->sctp_dst);
 
293
        }
121
294
        break;
122
295
 
123
296
    case OVS_KEY_ATTR_MPLS:
124
 
         set_mpls_lse(packet, nl_attr_get_be32(a));
125
 
         break;
 
297
        set_mpls_lse(packet, nl_attr_get_be32(a));
 
298
        break;
126
299
 
127
300
    case OVS_KEY_ATTR_ARP:
128
 
        set_arp(packet, nl_attr_get_unspec(a, sizeof(struct ovs_key_arp)));
 
301
        set_arp(packet, nl_attr_get(a), NULL);
 
302
        break;
 
303
 
 
304
    case OVS_KEY_ATTR_ND:
 
305
        if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) {
 
306
            const struct ovs_key_nd *nd_key
 
307
                   = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd));
 
308
            packet_set_nd(packet, nd_key->nd_target,
 
309
                          nd_key->nd_sll, nd_key->nd_tll);
 
310
        }
129
311
        break;
130
312
 
131
313
    case OVS_KEY_ATTR_DP_HASH:
143
325
    case OVS_KEY_ATTR_VLAN:
144
326
    case OVS_KEY_ATTR_ICMP:
145
327
    case OVS_KEY_ATTR_ICMPV6:
 
328
    case OVS_KEY_ATTR_TCP_FLAGS:
 
329
    case __OVS_KEY_ATTR_MAX:
 
330
    default:
 
331
        OVS_NOT_REACHED();
 
332
    }
 
333
}
 
334
 
 
335
#define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)
 
336
 
 
337
static void
 
338
odp_execute_masked_set_action(struct dp_packet *packet,
 
339
                              const struct nlattr *a)
 
340
{
 
341
    struct pkt_metadata *md = &packet->md;
 
342
    enum ovs_key_attr type = nl_attr_type(a);
 
343
    struct mpls_hdr *mh;
 
344
 
 
345
    switch (type) {
 
346
    case OVS_KEY_ATTR_PRIORITY:
 
347
        md->skb_priority = nl_attr_get_u32(a)
 
348
            | (md->skb_priority & ~*get_mask(a, uint32_t));
 
349
        break;
 
350
 
 
351
    case OVS_KEY_ATTR_SKB_MARK:
 
352
        md->pkt_mark = nl_attr_get_u32(a)
 
353
            | (md->pkt_mark & ~*get_mask(a, uint32_t));
 
354
        break;
 
355
 
 
356
    case OVS_KEY_ATTR_ETHERNET:
 
357
        odp_eth_set_addrs(packet, nl_attr_get(a),
 
358
                          get_mask(a, struct ovs_key_ethernet));
 
359
        break;
 
360
 
 
361
    case OVS_KEY_ATTR_IPV4:
 
362
        odp_set_ipv4(packet, nl_attr_get(a),
 
363
                     get_mask(a, struct ovs_key_ipv4));
 
364
        break;
 
365
 
 
366
    case OVS_KEY_ATTR_IPV6:
 
367
        odp_set_ipv6(packet, nl_attr_get(a),
 
368
                     get_mask(a, struct ovs_key_ipv6));
 
369
        break;
 
370
 
 
371
    case OVS_KEY_ATTR_TCP:
 
372
        odp_set_tcp(packet, nl_attr_get(a),
 
373
                    get_mask(a, struct ovs_key_tcp));
 
374
        break;
 
375
 
 
376
    case OVS_KEY_ATTR_UDP:
 
377
        odp_set_udp(packet, nl_attr_get(a),
 
378
                    get_mask(a, struct ovs_key_udp));
 
379
        break;
 
380
 
 
381
    case OVS_KEY_ATTR_SCTP:
 
382
        odp_set_sctp(packet, nl_attr_get(a),
 
383
                     get_mask(a, struct ovs_key_sctp));
 
384
        break;
 
385
 
 
386
    case OVS_KEY_ATTR_MPLS:
 
387
        mh = dp_packet_l2_5(packet);
 
388
        if (mh) {
 
389
            put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a)
 
390
                               | (get_16aligned_be32(&mh->mpls_lse)
 
391
                                  & ~*get_mask(a, ovs_be32)));
 
392
        }
 
393
        break;
 
394
 
 
395
    case OVS_KEY_ATTR_ARP:
 
396
        set_arp(packet, nl_attr_get(a),
 
397
                get_mask(a, struct ovs_key_arp));
 
398
        break;
 
399
 
146
400
    case OVS_KEY_ATTR_ND:
 
401
        odp_set_nd(packet, nl_attr_get(a),
 
402
                   get_mask(a, struct ovs_key_nd));
 
403
        break;
 
404
 
 
405
    case OVS_KEY_ATTR_DP_HASH:
 
406
        md->dp_hash = nl_attr_get_u32(a)
 
407
            | (md->dp_hash & ~*get_mask(a, uint32_t));
 
408
        break;
 
409
 
 
410
    case OVS_KEY_ATTR_RECIRC_ID:
 
411
        md->recirc_id = nl_attr_get_u32(a)
 
412
            | (md->recirc_id & ~*get_mask(a, uint32_t));
 
413
        break;
 
414
 
 
415
    case OVS_KEY_ATTR_TUNNEL:    /* Masked data not supported for tunnel. */
 
416
    case OVS_KEY_ATTR_UNSPEC:
 
417
    case OVS_KEY_ATTR_ENCAP:
 
418
    case OVS_KEY_ATTR_ETHERTYPE:
 
419
    case OVS_KEY_ATTR_IN_PORT:
 
420
    case OVS_KEY_ATTR_VLAN:
 
421
    case OVS_KEY_ATTR_ICMP:
 
422
    case OVS_KEY_ATTR_ICMPV6:
147
423
    case OVS_KEY_ATTR_TCP_FLAGS:
148
424
    case __OVS_KEY_ATTR_MAX:
149
425
    default:
152
428
}
153
429
 
154
430
static void
155
 
odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
156
 
                      struct pkt_metadata *,
157
 
                      const struct nlattr *actions, size_t actions_len,
158
 
                      odp_execute_cb dp_execute_action, bool more_actions);
159
 
 
160
 
static void
161
 
odp_execute_sample(void *dp, struct ofpbuf *packet, bool steal,
162
 
                   struct pkt_metadata *md, const struct nlattr *action,
163
 
                   odp_execute_cb dp_execute_action, bool more_actions)
 
431
odp_execute_sample(void *dp, struct dp_packet *packet, bool steal,
 
432
                   const struct nlattr *action,
 
433
                   odp_execute_cb dp_execute_action)
164
434
{
165
435
    const struct nlattr *subactions = NULL;
166
436
    const struct nlattr *a;
172
442
        switch ((enum ovs_sample_attr) type) {
173
443
        case OVS_SAMPLE_ATTR_PROBABILITY:
174
444
            if (random_uint32() >= nl_attr_get_u32(a)) {
 
445
                if (steal) {
 
446
                    dp_packet_delete(packet);
 
447
                }
175
448
                return;
176
449
            }
177
450
            break;
187
460
        }
188
461
    }
189
462
 
190
 
    odp_execute_actions__(dp, packet, steal, md, nl_attr_get(subactions),
191
 
                          nl_attr_get_size(subactions), dp_execute_action,
192
 
                          more_actions);
193
 
}
194
 
 
195
 
static void
196
 
odp_execute_actions__(void *dp, struct ofpbuf *packet, bool steal,
197
 
                      struct pkt_metadata *md,
198
 
                      const struct nlattr *actions, size_t actions_len,
199
 
                      odp_execute_cb dp_execute_action, bool more_actions)
 
463
    odp_execute_actions(dp, &packet, 1, steal, nl_attr_get(subactions),
 
464
                        nl_attr_get_size(subactions), dp_execute_action);
 
465
}
 
466
 
 
467
static bool
 
468
requires_datapath_assistance(const struct nlattr *a)
 
469
{
 
470
    enum ovs_action_attr type = nl_attr_type(a);
 
471
 
 
472
    switch (type) {
 
473
        /* These only make sense in the context of a datapath. */
 
474
    case OVS_ACTION_ATTR_OUTPUT:
 
475
    case OVS_ACTION_ATTR_TUNNEL_PUSH:
 
476
    case OVS_ACTION_ATTR_TUNNEL_POP:
 
477
    case OVS_ACTION_ATTR_USERSPACE:
 
478
    case OVS_ACTION_ATTR_RECIRC:
 
479
        return true;
 
480
 
 
481
    case OVS_ACTION_ATTR_SET:
 
482
    case OVS_ACTION_ATTR_SET_MASKED:
 
483
    case OVS_ACTION_ATTR_PUSH_VLAN:
 
484
    case OVS_ACTION_ATTR_POP_VLAN:
 
485
    case OVS_ACTION_ATTR_SAMPLE:
 
486
    case OVS_ACTION_ATTR_HASH:
 
487
    case OVS_ACTION_ATTR_PUSH_MPLS:
 
488
    case OVS_ACTION_ATTR_POP_MPLS:
 
489
        return false;
 
490
 
 
491
    case OVS_ACTION_ATTR_UNSPEC:
 
492
    case __OVS_ACTION_ATTR_MAX:
 
493
        OVS_NOT_REACHED();
 
494
    }
 
495
 
 
496
    return false;
 
497
}
 
498
 
 
499
void
 
500
odp_execute_actions(void *dp, struct dp_packet **packets, int cnt, bool steal,
 
501
                    const struct nlattr *actions, size_t actions_len,
 
502
                    odp_execute_cb dp_execute_action)
200
503
{
201
504
    const struct nlattr *a;
202
505
    unsigned int left;
 
506
    int i;
203
507
 
204
508
    NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
205
509
        int type = nl_attr_type(a);
 
510
        bool last_action = (left <= NLA_ALIGN(a->nla_len));
206
511
 
207
 
        switch ((enum ovs_action_attr) type) {
208
 
            /* These only make sense in the context of a datapath. */
209
 
        case OVS_ACTION_ATTR_OUTPUT:
210
 
        case OVS_ACTION_ATTR_USERSPACE:
211
 
        case OVS_ACTION_ATTR_RECIRC:
 
512
        if (requires_datapath_assistance(a)) {
212
513
            if (dp_execute_action) {
213
514
                /* Allow 'dp_execute_action' to steal the packet data if we do
214
515
                 * not need it any more. */
215
 
                bool may_steal = steal && (!more_actions
216
 
                                           && left <= NLA_ALIGN(a->nla_len)
217
 
                                           && type != OVS_ACTION_ATTR_RECIRC);
218
 
                dp_execute_action(dp, packet, md, a, may_steal);
 
516
                bool may_steal = steal && last_action;
 
517
 
 
518
                dp_execute_action(dp, packets, cnt, a, may_steal);
 
519
 
 
520
                if (last_action) {
 
521
                    /* We do not need to free the packets. dp_execute_actions()
 
522
                     * has stolen them */
 
523
                    return;
 
524
                }
219
525
            }
220
 
            break;
 
526
            continue;
 
527
        }
221
528
 
 
529
        switch ((enum ovs_action_attr) type) {
222
530
        case OVS_ACTION_ATTR_HASH: {
223
531
            const struct ovs_action_hash *hash_act = nl_attr_get(a);
224
532
 
230
538
                struct flow flow;
231
539
                uint32_t hash;
232
540
 
233
 
                flow_extract(packet, md, &flow);
234
 
                hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
235
 
                md->dp_hash = hash ? hash : 1;
 
541
                for (i = 0; i < cnt; i++) {
 
542
                    flow_extract(packets[i], &flow);
 
543
                    hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
 
544
 
 
545
                    packets[i]->md.dp_hash = hash;
 
546
                }
236
547
            } else {
237
548
                /* Assert on unknown hash algorithm.  */
238
549
                OVS_NOT_REACHED();
242
553
 
243
554
        case OVS_ACTION_ATTR_PUSH_VLAN: {
244
555
            const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
245
 
            eth_push_vlan(packet, htons(ETH_TYPE_VLAN), vlan->vlan_tci);
 
556
 
 
557
            for (i = 0; i < cnt; i++) {
 
558
                eth_push_vlan(packets[i], vlan->vlan_tpid, vlan->vlan_tci);
 
559
            }
246
560
            break;
247
561
        }
248
562
 
249
563
        case OVS_ACTION_ATTR_POP_VLAN:
250
 
            eth_pop_vlan(packet);
 
564
            for (i = 0; i < cnt; i++) {
 
565
                eth_pop_vlan(packets[i]);
 
566
            }
251
567
            break;
252
568
 
253
569
        case OVS_ACTION_ATTR_PUSH_MPLS: {
254
570
            const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
255
 
            push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
 
571
 
 
572
            for (i = 0; i < cnt; i++) {
 
573
                push_mpls(packets[i], mpls->mpls_ethertype, mpls->mpls_lse);
 
574
            }
256
575
            break;
257
576
         }
258
577
 
259
578
        case OVS_ACTION_ATTR_POP_MPLS:
260
 
            pop_mpls(packet, nl_attr_get_be16(a));
 
579
            for (i = 0; i < cnt; i++) {
 
580
                pop_mpls(packets[i], nl_attr_get_be16(a));
 
581
            }
261
582
            break;
262
583
 
263
584
        case OVS_ACTION_ATTR_SET:
264
 
            odp_execute_set_action(packet, nl_attr_get(a), md);
 
585
            for (i = 0; i < cnt; i++) {
 
586
                odp_execute_set_action(packets[i], nl_attr_get(a));
 
587
            }
 
588
            break;
 
589
 
 
590
        case OVS_ACTION_ATTR_SET_MASKED:
 
591
            for (i = 0; i < cnt; i++) {
 
592
                odp_execute_masked_set_action(packets[i], nl_attr_get(a));
 
593
            }
265
594
            break;
266
595
 
267
596
        case OVS_ACTION_ATTR_SAMPLE:
268
 
            odp_execute_sample(dp, packet, steal, md, a, dp_execute_action,
269
 
                               more_actions || left > NLA_ALIGN(a->nla_len));
 
597
            for (i = 0; i < cnt; i++) {
 
598
                odp_execute_sample(dp, packets[i], steal && last_action, a,
 
599
                                   dp_execute_action);
 
600
            }
 
601
 
 
602
            if (last_action) {
 
603
                /* We do not need to free the packets. odp_execute_sample() has
 
604
                 * stolen them*/
 
605
                return;
 
606
            }
270
607
            break;
271
608
 
 
609
        case OVS_ACTION_ATTR_OUTPUT:
 
610
        case OVS_ACTION_ATTR_TUNNEL_PUSH:
 
611
        case OVS_ACTION_ATTR_TUNNEL_POP:
 
612
        case OVS_ACTION_ATTR_USERSPACE:
 
613
        case OVS_ACTION_ATTR_RECIRC:
272
614
        case OVS_ACTION_ATTR_UNSPEC:
273
615
        case __OVS_ACTION_ATTR_MAX:
274
616
            OVS_NOT_REACHED();
275
617
        }
276
618
    }
277
 
}
278
 
 
279
 
void
280
 
odp_execute_actions(void *dp, struct ofpbuf *packet, bool steal,
281
 
                    struct pkt_metadata *md,
282
 
                    const struct nlattr *actions, size_t actions_len,
283
 
                    odp_execute_cb dp_execute_action)
284
 
{
285
 
    odp_execute_actions__(dp, packet, steal, md, actions, actions_len,
286
 
                          dp_execute_action, false);
287
 
 
288
 
    if (!actions_len && steal) {
289
 
        /* Drop action. */
290
 
        ofpbuf_delete(packet);
 
619
 
 
620
    if (steal) {
 
621
        for (i = 0; i < cnt; i++) {
 
622
            dp_packet_delete(packets[i]);
 
623
        }
291
624
    }
292
625
}