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

« back to all changes in this revision

Viewing changes to datapath/actions.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:
32
32
#include <net/ipv6.h>
33
33
#include <net/checksum.h>
34
34
#include <net/dsfield.h>
 
35
#include <net/mpls.h>
35
36
#include <net/sctp/checksum.h>
36
37
 
37
38
#include "datapath.h"
 
39
#include "gso.h"
38
40
#include "vlan.h"
39
41
#include "vport.h"
40
42
 
41
43
static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
 
44
                              struct sw_flow_key *key,
42
45
                              const struct nlattr *attr, int len);
43
46
 
44
 
static int make_writable(struct sk_buff *skb, int write_len)
45
 
{
46
 
        if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
47
 
                return 0;
48
 
 
49
 
        return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
50
 
}
51
 
 
52
 
/* remove VLAN header from packet and update csum accordingly. */
53
 
static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
54
 
{
55
 
        struct vlan_hdr *vhdr;
56
 
        int err;
57
 
 
58
 
        err = make_writable(skb, VLAN_ETH_HLEN);
59
 
        if (unlikely(err))
60
 
                return err;
61
 
 
62
 
        if (skb->ip_summed == CHECKSUM_COMPLETE)
63
 
                skb->csum = csum_sub(skb->csum, csum_partial(skb->data
64
 
                                        + (2 * ETH_ALEN), VLAN_HLEN, 0));
65
 
 
66
 
        vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
67
 
        *current_tci = vhdr->h_vlan_TCI;
68
 
 
69
 
        memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
70
 
        __skb_pull(skb, VLAN_HLEN);
71
 
 
72
 
        vlan_set_encap_proto(skb, vhdr);
73
 
        skb->mac_header += VLAN_HLEN;
74
 
        skb_reset_mac_len(skb);
75
 
 
76
 
        return 0;
77
 
}
78
 
 
79
 
static int pop_vlan(struct sk_buff *skb)
80
 
{
81
 
        __be16 tci;
82
 
        int err;
83
 
 
84
 
        if (likely(vlan_tx_tag_present(skb))) {
85
 
                vlan_set_tci(skb, 0);
86
 
        } else {
87
 
                if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
88
 
                             skb->len < VLAN_ETH_HLEN))
89
 
                        return 0;
90
 
 
91
 
                err = __pop_vlan_tci(skb, &tci);
92
 
                if (err)
93
 
                        return err;
94
 
        }
95
 
        /* move next vlan tag to hw accel tag */
96
 
        if (likely(skb->protocol != htons(ETH_P_8021Q) ||
97
 
                   skb->len < VLAN_ETH_HLEN))
98
 
                return 0;
99
 
 
100
 
        err = __pop_vlan_tci(skb, &tci);
101
 
        if (unlikely(err))
102
 
                return err;
103
 
 
104
 
        __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
105
 
        return 0;
106
 
}
107
 
 
108
 
static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
109
 
{
110
 
        if (unlikely(vlan_tx_tag_present(skb))) {
111
 
                u16 current_tag;
112
 
 
113
 
                /* push down current VLAN tag */
114
 
                current_tag = vlan_tx_tag_get(skb);
115
 
 
116
 
                if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
117
 
                        return -ENOMEM;
118
 
 
119
 
                if (skb->ip_summed == CHECKSUM_COMPLETE)
120
 
                        skb->csum = csum_add(skb->csum, csum_partial(skb->data
121
 
                                        + (2 * ETH_ALEN), VLAN_HLEN, 0));
122
 
 
123
 
        }
124
 
        __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
125
 
        return 0;
126
 
}
127
 
 
128
 
static int set_eth_addr(struct sk_buff *skb,
129
 
                        const struct ovs_key_ethernet *eth_key)
130
 
{
131
 
        int err;
132
 
        err = make_writable(skb, ETH_HLEN);
 
47
struct deferred_action {
 
48
        struct sk_buff *skb;
 
49
        const struct nlattr *actions;
 
50
 
 
51
        /* Store pkt_key clone when creating deferred action. */
 
52
        struct sw_flow_key pkt_key;
 
53
};
 
54
 
 
55
#define DEFERRED_ACTION_FIFO_SIZE 10
 
56
struct action_fifo {
 
57
        int head;
 
58
        int tail;
 
59
        /* Deferred action fifo queue storage. */
 
60
        struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
 
61
};
 
62
 
 
63
static struct action_fifo __percpu *action_fifos;
 
64
#define EXEC_ACTIONS_LEVEL_LIMIT 4   /* limit used to detect packet
 
65
                                      * looping by the network stack
 
66
                                      */
 
67
static DEFINE_PER_CPU(int, exec_actions_level);
 
68
 
 
69
static void action_fifo_init(struct action_fifo *fifo)
 
70
{
 
71
        fifo->head = 0;
 
72
        fifo->tail = 0;
 
73
}
 
74
 
 
75
static bool action_fifo_is_empty(const struct action_fifo *fifo)
 
76
{
 
77
        return (fifo->head == fifo->tail);
 
78
}
 
79
 
 
80
static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
 
81
{
 
82
        if (action_fifo_is_empty(fifo))
 
83
                return NULL;
 
84
 
 
85
        return &fifo->fifo[fifo->tail++];
 
86
}
 
87
 
 
88
static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
 
89
{
 
90
        if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
 
91
                return NULL;
 
92
 
 
93
        return &fifo->fifo[fifo->head++];
 
94
}
 
95
 
 
96
/* Return queue entry if fifo is not full */
 
97
static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
 
98
                                                    const struct sw_flow_key *key,
 
99
                                                    const struct nlattr *attr)
 
100
{
 
101
        struct action_fifo *fifo;
 
102
        struct deferred_action *da;
 
103
 
 
104
        fifo = this_cpu_ptr(action_fifos);
 
105
        da = action_fifo_put(fifo);
 
106
        if (da) {
 
107
                da->skb = skb;
 
108
                da->actions = attr;
 
109
                da->pkt_key = *key;
 
110
        }
 
111
 
 
112
        return da;
 
113
}
 
114
 
 
115
static void invalidate_flow_key(struct sw_flow_key *key)
 
116
{
 
117
        key->eth.type = htons(0);
 
118
}
 
119
 
 
120
static bool is_flow_key_valid(const struct sw_flow_key *key)
 
121
{
 
122
        return !!key->eth.type;
 
123
}
 
124
 
 
125
static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
 
126
                     const struct ovs_action_push_mpls *mpls)
 
127
{
 
128
        __be32 *new_mpls_lse;
 
129
        struct ethhdr *hdr;
 
130
 
 
131
        /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
 
132
        if (skb_encapsulation(skb))
 
133
                return -ENOTSUPP;
 
134
 
 
135
        if (skb_cow_head(skb, MPLS_HLEN) < 0)
 
136
                return -ENOMEM;
 
137
 
 
138
        skb_push(skb, MPLS_HLEN);
 
139
        memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
 
140
                skb->mac_len);
 
141
        skb_reset_mac_header(skb);
 
142
 
 
143
        new_mpls_lse = (__be32 *)skb_mpls_header(skb);
 
144
        *new_mpls_lse = mpls->mpls_lse;
 
145
 
 
146
        if (skb->ip_summed == CHECKSUM_COMPLETE)
 
147
                skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
 
148
                                                             MPLS_HLEN, 0));
 
149
 
 
150
        hdr = eth_hdr(skb);
 
151
        hdr->h_proto = mpls->mpls_ethertype;
 
152
        if (!ovs_skb_get_inner_protocol(skb))
 
153
                ovs_skb_set_inner_protocol(skb, skb->protocol);
 
154
        skb->protocol = mpls->mpls_ethertype;
 
155
 
 
156
        invalidate_flow_key(key);
 
157
        return 0;
 
158
}
 
159
 
 
160
static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
 
161
                    const __be16 ethertype)
 
162
{
 
163
        struct ethhdr *hdr;
 
164
        int err;
 
165
 
 
166
        err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
 
167
        if (unlikely(err))
 
168
                return err;
 
169
 
 
170
        if (skb->ip_summed == CHECKSUM_COMPLETE)
 
171
                skb->csum = csum_sub(skb->csum,
 
172
                                     csum_partial(skb_mpls_header(skb),
 
173
                                                  MPLS_HLEN, 0));
 
174
 
 
175
        memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
 
176
                skb->mac_len);
 
177
 
 
178
        __skb_pull(skb, MPLS_HLEN);
 
179
        skb_reset_mac_header(skb);
 
180
 
 
181
        /* skb_mpls_header() is used to locate the ethertype
 
182
         * field correctly in the presence of VLAN tags.
 
183
         */
 
184
        hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
 
185
        hdr->h_proto = ethertype;
 
186
        if (eth_p_mpls(skb->protocol))
 
187
                skb->protocol = ethertype;
 
188
 
 
189
        invalidate_flow_key(key);
 
190
        return 0;
 
191
}
 
192
 
 
193
/* 'KEY' must not have any bits set outside of the 'MASK' */
 
194
#define MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
 
195
#define SET_MASKED(OLD, KEY, MASK) ((OLD) = MASKED(OLD, KEY, MASK))
 
196
 
 
197
static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
198
                    const __be32 *mpls_lse, const __be32 *mask)
 
199
{
 
200
        __be32 *stack;
 
201
        __be32 lse;
 
202
        int err;
 
203
 
 
204
        err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
 
205
        if (unlikely(err))
 
206
                return err;
 
207
 
 
208
        stack = (__be32 *)skb_mpls_header(skb);
 
209
        lse = MASKED(*stack, *mpls_lse, *mask);
 
210
        if (skb->ip_summed == CHECKSUM_COMPLETE) {
 
211
                __be32 diff[] = { ~(*stack), lse };
 
212
 
 
213
                skb->csum = ~csum_partial((char *)diff, sizeof(diff),
 
214
                                          ~skb->csum);
 
215
        }
 
216
 
 
217
        *stack = lse;
 
218
        flow_key->mpls.top_lse = lse;
 
219
        return 0;
 
220
}
 
221
 
 
222
static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
 
223
{
 
224
        int err;
 
225
 
 
226
        err = skb_vlan_pop(skb);
 
227
        if (skb_vlan_tag_present(skb))
 
228
                invalidate_flow_key(key);
 
229
        else
 
230
                key->eth.tci = 0;
 
231
 
 
232
        return err;
 
233
}
 
234
 
 
235
static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
 
236
                     const struct ovs_action_push_vlan *vlan)
 
237
{
 
238
        if (skb_vlan_tag_present(skb))
 
239
                invalidate_flow_key(key);
 
240
        else
 
241
                key->eth.tci = vlan->vlan_tci;
 
242
 
 
243
        return skb_vlan_push(skb, vlan->vlan_tpid,
 
244
                             ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
 
245
}
 
246
 
 
247
/* 'src' is already properly masked. */
 
248
static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
 
249
{
 
250
        u16 *dst = (u16 *)dst_;
 
251
        const u16 *src = (const u16 *)src_;
 
252
        const u16 *mask = (const u16 *)mask_;
 
253
 
 
254
        SET_MASKED(dst[0], src[0], mask[0]);
 
255
        SET_MASKED(dst[1], src[1], mask[1]);
 
256
        SET_MASKED(dst[2], src[2], mask[2]);
 
257
}
 
258
 
 
259
static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
260
                        const struct ovs_key_ethernet *key,
 
261
                        const struct ovs_key_ethernet *mask)
 
262
{
 
263
        int err;
 
264
 
 
265
        err = skb_ensure_writable(skb, ETH_HLEN);
133
266
        if (unlikely(err))
134
267
                return err;
135
268
 
136
269
        skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
137
270
 
138
 
        ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
139
 
        ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
 
271
        ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
 
272
                               mask->eth_src);
 
273
        ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
 
274
                               mask->eth_dst);
140
275
 
141
276
        ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
142
277
 
 
278
        ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
 
279
        ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
143
280
        return 0;
144
281
}
145
282
 
146
283
static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
147
 
                                __be32 *addr, __be32 new_addr)
 
284
                        __be32 *addr, __be32 new_addr)
148
285
{
149
286
        int transport_len = skb->len - skb_transport_offset(skb);
150
287
 
197
334
        }
198
335
}
199
336
 
 
337
static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
 
338
                           const __be32 mask[4], __be32 masked[4])
 
339
{
 
340
        masked[0] = MASKED(old[0], addr[0], mask[0]);
 
341
        masked[1] = MASKED(old[1], addr[1], mask[1]);
 
342
        masked[2] = MASKED(old[2], addr[2], mask[2]);
 
343
        masked[3] = MASKED(old[3], addr[3], mask[3]);
 
344
}
 
345
 
200
346
static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
201
347
                          __be32 addr[4], const __be32 new_addr[4],
202
348
                          bool recalculate_csum)
203
349
{
204
 
        if (recalculate_csum)
 
350
        if (likely(recalculate_csum))
205
351
                update_ipv6_checksum(skb, l4_proto, addr, new_addr);
206
352
 
207
353
        skb_clear_hash(skb);
208
354
        memcpy(addr, new_addr, sizeof(__be32[4]));
209
355
}
210
356
 
211
 
static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
212
 
{
213
 
        nh->priority = tc >> 4;
214
 
        nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
215
 
}
216
 
 
217
 
static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
218
 
{
219
 
        nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
220
 
        nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
221
 
        nh->flow_lbl[2] = fl & 0x000000FF;
222
 
}
223
 
 
224
 
static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
225
 
{
 
357
static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
 
358
{
 
359
        /* Bits 21-24 are always unmasked, so this retains their values. */
 
360
        SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
 
361
        SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
 
362
        SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
 
363
}
 
364
 
 
365
static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
 
366
                       u8 mask)
 
367
{
 
368
        new_ttl = MASKED(nh->ttl, new_ttl, mask);
 
369
 
226
370
        csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
227
371
        nh->ttl = new_ttl;
228
372
}
229
373
 
230
 
static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
 
374
static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
375
                    const struct ovs_key_ipv4 *key,
 
376
                    const struct ovs_key_ipv4 *mask)
231
377
{
232
378
        struct iphdr *nh;
 
379
        __be32 new_addr;
233
380
        int err;
234
381
 
235
 
        err = make_writable(skb, skb_network_offset(skb) +
236
 
                                 sizeof(struct iphdr));
 
382
        err = skb_ensure_writable(skb, skb_network_offset(skb) +
 
383
                                  sizeof(struct iphdr));
237
384
        if (unlikely(err))
238
385
                return err;
239
386
 
240
387
        nh = ip_hdr(skb);
241
388
 
242
 
        if (ipv4_key->ipv4_src != nh->saddr)
243
 
                set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
244
 
 
245
 
        if (ipv4_key->ipv4_dst != nh->daddr)
246
 
                set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
247
 
 
248
 
        if (ipv4_key->ipv4_tos != nh->tos)
249
 
                ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
250
 
 
251
 
        if (ipv4_key->ipv4_ttl != nh->ttl)
252
 
                set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
 
389
        /* Setting an IP addresses is typically only a side effect of
 
390
         * matching on them in the current userspace implementation, so it
 
391
         * makes sense to check if the value actually changed.
 
392
         */
 
393
        if (mask->ipv4_src) {
 
394
                new_addr = MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
 
395
 
 
396
                if (unlikely(new_addr != nh->saddr)) {
 
397
                        set_ip_addr(skb, nh, &nh->saddr, new_addr);
 
398
                        flow_key->ipv4.addr.src = new_addr;
 
399
                }
 
400
        }
 
401
        if (mask->ipv4_dst) {
 
402
                new_addr = MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
 
403
 
 
404
                if (unlikely(new_addr != nh->daddr)) {
 
405
                        set_ip_addr(skb, nh, &nh->daddr, new_addr);
 
406
                        flow_key->ipv4.addr.dst = new_addr;
 
407
                }
 
408
        }
 
409
        if (mask->ipv4_tos) {
 
410
                ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
 
411
                flow_key->ip.tos = nh->tos;
 
412
        }
 
413
        if (mask->ipv4_ttl) {
 
414
                set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
 
415
                flow_key->ip.ttl = nh->ttl;
 
416
        }
253
417
 
254
418
        return 0;
255
419
}
256
420
 
257
 
static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
 
421
static bool is_ipv6_mask_nonzero(const __be32 addr[4])
 
422
{
 
423
        return !!(addr[0] | addr[1] | addr[2] | addr[3]);
 
424
}
 
425
 
 
426
static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
427
                    const struct ovs_key_ipv6 *key,
 
428
                    const struct ovs_key_ipv6 *mask)
258
429
{
259
430
        struct ipv6hdr *nh;
260
431
        int err;
261
 
        __be32 *saddr;
262
 
        __be32 *daddr;
263
432
 
264
 
        err = make_writable(skb, skb_network_offset(skb) +
265
 
                            sizeof(struct ipv6hdr));
 
433
        err = skb_ensure_writable(skb, skb_network_offset(skb) +
 
434
                                  sizeof(struct ipv6hdr));
266
435
        if (unlikely(err))
267
436
                return err;
268
437
 
269
438
        nh = ipv6_hdr(skb);
270
 
        saddr = (__be32 *)&nh->saddr;
271
 
        daddr = (__be32 *)&nh->daddr;
272
 
 
273
 
        if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
274
 
                set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
275
 
                              ipv6_key->ipv6_src, true);
276
 
 
277
 
        if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
 
439
 
 
440
        /* Setting an IP addresses is typically only a side effect of
 
441
         * matching on them in the current userspace implementation, so it
 
442
         * makes sense to check if the value actually changed.
 
443
         */
 
444
        if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
 
445
                __be32 *saddr = (__be32 *)&nh->saddr;
 
446
                __be32 masked[4];
 
447
 
 
448
                mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
 
449
 
 
450
                if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
 
451
                        set_ipv6_addr(skb, key->ipv6_proto, saddr, masked,
 
452
                                      true);
 
453
                        memcpy(&flow_key->ipv6.addr.src, masked,
 
454
                               sizeof(flow_key->ipv6.addr.src));
 
455
                }
 
456
        }
 
457
        if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
278
458
                unsigned int offset = 0;
279
459
                int flags = IP6_FH_F_SKIP_RH;
280
460
                bool recalc_csum = true;
281
 
 
282
 
                if (ipv6_ext_hdr(nh->nexthdr))
283
 
                        recalc_csum = ipv6_find_hdr(skb, &offset,
284
 
                                                    NEXTHDR_ROUTING, NULL,
285
 
                                                    &flags) != NEXTHDR_ROUTING;
286
 
 
287
 
                set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
288
 
                              ipv6_key->ipv6_dst, recalc_csum);
289
 
        }
290
 
 
291
 
        set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
292
 
        set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
293
 
        nh->hop_limit = ipv6_key->ipv6_hlimit;
294
 
 
 
461
                __be32 *daddr = (__be32 *)&nh->daddr;
 
462
                __be32 masked[4];
 
463
 
 
464
                mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
 
465
 
 
466
                if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
 
467
                        if (ipv6_ext_hdr(nh->nexthdr))
 
468
                                recalc_csum = (ipv6_find_hdr(skb, &offset,
 
469
                                                             NEXTHDR_ROUTING,
 
470
                                                             NULL, &flags)
 
471
                                               != NEXTHDR_ROUTING);
 
472
 
 
473
                        set_ipv6_addr(skb, key->ipv6_proto, daddr, masked,
 
474
                                      recalc_csum);
 
475
                        memcpy(&flow_key->ipv6.addr.dst, masked,
 
476
                               sizeof(flow_key->ipv6.addr.dst));
 
477
                }
 
478
        }
 
479
        if (mask->ipv6_tclass) {
 
480
                ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
 
481
                flow_key->ip.tos = ipv6_get_dsfield(nh);
 
482
        }
 
483
        if (mask->ipv6_label) {
 
484
                set_ipv6_fl(nh, ntohl(key->ipv6_label),
 
485
                            ntohl(mask->ipv6_label));
 
486
                flow_key->ipv6.label =
 
487
                    *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
 
488
        }
 
489
        if (mask->ipv6_hlimit) {
 
490
                SET_MASKED(nh->hop_limit, key->ipv6_hlimit, mask->ipv6_hlimit);
 
491
                flow_key->ip.ttl = nh->hop_limit;
 
492
        }
295
493
        return 0;
296
494
}
297
495
 
298
 
/* Must follow make_writable() since that can move the skb data. */
 
496
/* Must follow skb_ensure_writable() since that can move the skb data. */
299
497
static void set_tp_port(struct sk_buff *skb, __be16 *port,
300
 
                         __be16 new_port, __sum16 *check)
 
498
                        __be16 new_port, __sum16 *check)
301
499
{
302
500
        inet_proto_csum_replace2(check, skb, *port, new_port, 0);
303
501
        *port = new_port;
304
 
        skb_clear_hash(skb);
305
502
}
306
503
 
307
 
static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
 
504
static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
505
                   const struct ovs_key_udp *key,
 
506
                   const struct ovs_key_udp *mask)
308
507
{
309
 
        struct udphdr *uh = udp_hdr(skb);
 
508
        struct udphdr *uh;
 
509
        __be16 src, dst;
 
510
        int err;
 
511
 
 
512
        err = skb_ensure_writable(skb, skb_transport_offset(skb) +
 
513
                                  sizeof(struct udphdr));
 
514
        if (unlikely(err))
 
515
                return err;
 
516
 
 
517
        uh = udp_hdr(skb);
 
518
        /* Either of the masks is non-zero, so do not bother checking them. */
 
519
        src = MASKED(uh->source, key->udp_src, mask->udp_src);
 
520
        dst = MASKED(uh->dest, key->udp_dst, mask->udp_dst);
310
521
 
311
522
        if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
312
 
                set_tp_port(skb, port, new_port, &uh->check);
 
523
                if (likely(src != uh->source)) {
 
524
                        set_tp_port(skb, &uh->source, src, &uh->check);
 
525
                        flow_key->tp.src = src;
 
526
                }
 
527
                if (likely(dst != uh->dest)) {
 
528
                        set_tp_port(skb, &uh->dest, dst, &uh->check);
 
529
                        flow_key->tp.dst = dst;
 
530
                }
313
531
 
314
 
                if (!uh->check)
 
532
                if (unlikely(!uh->check))
315
533
                        uh->check = CSUM_MANGLED_0;
316
534
        } else {
317
 
                *port = new_port;
318
 
                skb_clear_hash(skb);
 
535
                uh->source = src;
 
536
                uh->dest = dst;
 
537
                flow_key->tp.src = src;
 
538
                flow_key->tp.dst = dst;
319
539
        }
320
 
}
321
 
 
322
 
static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
323
 
{
324
 
        struct udphdr *uh;
325
 
        int err;
326
 
 
327
 
        err = make_writable(skb, skb_transport_offset(skb) +
328
 
                                 sizeof(struct udphdr));
329
 
        if (unlikely(err))
330
 
                return err;
331
 
 
332
 
        uh = udp_hdr(skb);
333
 
        if (udp_port_key->udp_src != uh->source)
334
 
                set_udp_port(skb, &uh->source, udp_port_key->udp_src);
335
 
 
336
 
        if (udp_port_key->udp_dst != uh->dest)
337
 
                set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
 
540
 
 
541
        skb_clear_hash(skb);
338
542
 
339
543
        return 0;
340
544
}
341
545
 
342
 
static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
 
546
static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
547
                   const struct ovs_key_tcp *key,
 
548
                   const struct ovs_key_tcp *mask)
343
549
{
344
550
        struct tcphdr *th;
 
551
        __be16 src, dst;
345
552
        int err;
346
553
 
347
 
        err = make_writable(skb, skb_transport_offset(skb) +
348
 
                                 sizeof(struct tcphdr));
 
554
        err = skb_ensure_writable(skb, skb_transport_offset(skb) +
 
555
                                  sizeof(struct tcphdr));
349
556
        if (unlikely(err))
350
557
                return err;
351
558
 
352
559
        th = tcp_hdr(skb);
353
 
        if (tcp_port_key->tcp_src != th->source)
354
 
                set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
355
560
 
356
 
        if (tcp_port_key->tcp_dst != th->dest)
357
 
                set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
 
561
        src = MASKED(th->source, key->tcp_src, mask->tcp_src);
 
562
        if (likely(src != th->source)) {
 
563
                set_tp_port(skb, &th->source, src, &th->check);
 
564
                flow_key->tp.src = src;
 
565
        }
 
566
        dst = MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
 
567
        if (likely(dst != th->dest)) {
 
568
                set_tp_port(skb, &th->dest, dst, &th->check);
 
569
                flow_key->tp.dst = dst;
 
570
        }
 
571
        skb_clear_hash(skb);
358
572
 
359
573
        return 0;
360
574
}
361
575
 
362
 
static int set_sctp(struct sk_buff *skb,
363
 
                     const struct ovs_key_sctp *sctp_port_key)
 
576
static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
 
577
                    const struct ovs_key_sctp *key,
 
578
                    const struct ovs_key_sctp *mask)
364
579
{
 
580
        unsigned int sctphoff = skb_transport_offset(skb);
365
581
        struct sctphdr *sh;
 
582
        __le32 old_correct_csum, new_csum, old_csum;
366
583
        int err;
367
 
        unsigned int sctphoff = skb_transport_offset(skb);
368
584
 
369
 
        err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
 
585
        err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
370
586
        if (unlikely(err))
371
587
                return err;
372
588
 
373
589
        sh = sctp_hdr(skb);
374
 
        if (sctp_port_key->sctp_src != sh->source ||
375
 
            sctp_port_key->sctp_dst != sh->dest) {
376
 
                __le32 old_correct_csum, new_csum, old_csum;
377
 
 
378
 
                old_csum = sh->checksum;
379
 
                old_correct_csum = sctp_compute_cksum(skb, sctphoff);
380
 
 
381
 
                sh->source = sctp_port_key->sctp_src;
382
 
                sh->dest = sctp_port_key->sctp_dst;
383
 
 
384
 
                new_csum = sctp_compute_cksum(skb, sctphoff);
385
 
 
386
 
                /* Carry any checksum errors through. */
387
 
                sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
388
 
 
389
 
                skb_clear_hash(skb);
390
 
        }
 
590
 
 
591
        old_csum = sh->checksum;
 
592
        old_correct_csum = sctp_compute_cksum(skb, sctphoff);
 
593
 
 
594
        sh->source = MASKED(sh->source, key->sctp_src, mask->sctp_src);
 
595
        sh->dest = MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
 
596
 
 
597
        new_csum = sctp_compute_cksum(skb, sctphoff);
 
598
 
 
599
        /* Carry any checksum errors through. */
 
600
        sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
 
601
 
 
602
        skb_clear_hash(skb);
 
603
        flow_key->tp.src = sh->source;
 
604
        flow_key->tp.dst = sh->dest;
391
605
 
392
606
        return 0;
393
607
}
394
608
 
395
 
static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
 
609
static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
396
610
{
397
 
        struct vport *vport;
398
 
 
399
 
        if (unlikely(!skb))
400
 
                return -ENOMEM;
401
 
 
402
 
        vport = ovs_vport_rcu(dp, out_port);
403
 
        if (unlikely(!vport)) {
 
611
        struct vport *vport = ovs_vport_rcu(dp, out_port);
 
612
 
 
613
        if (likely(vport))
 
614
                ovs_vport_send(vport, skb);
 
615
        else
404
616
                kfree_skb(skb);
405
 
                return -ENODEV;
406
 
        }
407
 
 
408
 
        ovs_vport_send(vport, skb);
409
 
        return 0;
410
617
}
411
618
 
412
619
static int output_userspace(struct datapath *dp, struct sk_buff *skb,
413
 
                            const struct nlattr *attr)
 
620
                            struct sw_flow_key *key, const struct nlattr *attr)
414
621
{
 
622
        struct ovs_tunnel_info info;
415
623
        struct dp_upcall_info upcall;
416
624
        const struct nlattr *a;
417
625
        int rem;
418
626
 
419
 
        BUG_ON(!OVS_CB(skb)->pkt_key);
420
 
 
421
627
        upcall.cmd = OVS_PACKET_CMD_ACTION;
422
 
        upcall.key = OVS_CB(skb)->pkt_key;
423
628
        upcall.userdata = NULL;
424
629
        upcall.portid = 0;
 
630
        upcall.egress_tun_info = NULL;
425
631
 
426
632
        for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
427
633
                 a = nla_next(a, &rem)) {
433
639
                case OVS_USERSPACE_ATTR_PID:
434
640
                        upcall.portid = nla_get_u32(a);
435
641
                        break;
 
642
 
 
643
                case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
 
644
                        /* Get out tunnel info. */
 
645
                        struct vport *vport;
 
646
 
 
647
                        vport = ovs_vport_rcu(dp, nla_get_u32(a));
 
648
                        if (vport) {
 
649
                                int err;
 
650
 
 
651
                                err = ovs_vport_get_egress_tun_info(vport, skb,
 
652
                                                                    &info);
 
653
                                if (!err)
 
654
                                        upcall.egress_tun_info = &info;
 
655
                        }
 
656
                        break;
436
657
                }
 
658
 
 
659
                } /* End of switch. */
437
660
        }
438
661
 
439
 
        return ovs_dp_upcall(dp, skb, &upcall);
440
 
}
441
 
 
442
 
static bool last_action(const struct nlattr *a, int rem)
443
 
{
444
 
        return a->nla_len == rem;
 
662
        return ovs_dp_upcall(dp, skb, key, &upcall);
445
663
}
446
664
 
447
665
static int sample(struct datapath *dp, struct sk_buff *skb,
448
 
                  const struct nlattr *attr)
 
666
                  struct sw_flow_key *key, const struct nlattr *attr)
449
667
{
450
668
        const struct nlattr *acts_list = NULL;
451
669
        const struct nlattr *a;
475
693
        /* The only known usage of sample action is having a single user-space
476
694
         * action. Treat this usage as a special case.
477
695
         * The output_userspace() should clone the skb to be sent to the
478
 
         * user space. This skb will be consumed by its caller. */
 
696
         * user space. This skb will be consumed by its caller.
 
697
         */
479
698
        if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
480
 
                   last_action(a, rem)))
481
 
                return output_userspace(dp, skb, a);
 
699
                   nla_is_last(a, rem)))
 
700
                return output_userspace(dp, skb, key, a);
482
701
 
483
702
        skb = skb_clone(skb, GFP_ATOMIC);
484
703
        if (!skb)
485
704
                /* Skip the sample action when out of memory. */
486
705
                return 0;
487
706
 
488
 
        return do_execute_actions(dp, skb, a, rem);
 
707
        if (!add_deferred_actions(skb, key, a)) {
 
708
                if (net_ratelimit())
 
709
                        pr_warn("%s: deferred actions limit reached, dropping sample action\n",
 
710
                                ovs_dp_name(dp));
 
711
 
 
712
                kfree_skb(skb);
 
713
        }
 
714
        return 0;
489
715
}
490
716
 
491
 
static void execute_hash(struct sk_buff *skb, const struct nlattr *attr)
 
717
static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
 
718
                         const struct nlattr *attr)
492
719
{
493
 
        struct sw_flow_key *key = OVS_CB(skb)->pkt_key;
494
720
        struct ovs_action_hash *hash_act = nla_data(attr);
495
721
        u32 hash = 0;
496
722
 
504
730
}
505
731
 
506
732
static int execute_set_action(struct sk_buff *skb,
507
 
                                 const struct nlattr *nested_attr)
 
733
                              struct sw_flow_key *flow_key,
 
734
                              const struct nlattr *a)
 
735
{
 
736
        /* Only tunnel set execution is supported without a mask. */
 
737
        if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
 
738
                OVS_CB(skb)->egress_tun_info = nla_data(a);
 
739
                return 0;
 
740
        }
 
741
 
 
742
        return -EINVAL;
 
743
 
 
744
}
 
745
 
 
746
/* Mask is at the midpoint of the data. */
 
747
#define get_mask(a, type) ((const type)nla_data(a) + 1)
 
748
 
 
749
static int execute_masked_set_action(struct sk_buff *skb,
 
750
                                     struct sw_flow_key *flow_key,
 
751
                                     const struct nlattr *a)
508
752
{
509
753
        int err = 0;
510
754
 
511
 
        switch (nla_type(nested_attr)) {
 
755
        switch (nla_type(a)) {
512
756
        case OVS_KEY_ATTR_PRIORITY:
513
 
                skb->priority = nla_get_u32(nested_attr);
 
757
                SET_MASKED(skb->priority, nla_get_u32(a), *get_mask(a, u32 *));
 
758
                flow_key->phy.priority = skb->priority;
514
759
                break;
515
760
 
516
761
        case OVS_KEY_ATTR_SKB_MARK:
517
 
                skb->mark = nla_get_u32(nested_attr);
 
762
                SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
 
763
                flow_key->phy.skb_mark = skb->mark;
518
764
                break;
519
765
 
520
 
        case OVS_KEY_ATTR_IPV4_TUNNEL:
521
 
                OVS_CB(skb)->tun_key = nla_data(nested_attr);
 
766
        case OVS_KEY_ATTR_TUNNEL_INFO:
 
767
                /* Masked data not supported for tunnel. */
 
768
                err = -EINVAL;
522
769
                break;
523
770
 
524
771
        case OVS_KEY_ATTR_ETHERNET:
525
 
                err = set_eth_addr(skb, nla_data(nested_attr));
 
772
                err = set_eth_addr(skb, flow_key, nla_data(a),
 
773
                                   get_mask(a, struct ovs_key_ethernet *));
526
774
                break;
527
775
 
528
776
        case OVS_KEY_ATTR_IPV4:
529
 
                err = set_ipv4(skb, nla_data(nested_attr));
 
777
                err = set_ipv4(skb, flow_key, nla_data(a),
 
778
                               get_mask(a, struct ovs_key_ipv4 *));
530
779
                break;
531
780
 
532
781
        case OVS_KEY_ATTR_IPV6:
533
 
                err = set_ipv6(skb, nla_data(nested_attr));
 
782
                err = set_ipv6(skb, flow_key, nla_data(a),
 
783
                               get_mask(a, struct ovs_key_ipv6 *));
534
784
                break;
535
785
 
536
786
        case OVS_KEY_ATTR_TCP:
537
 
                err = set_tcp(skb, nla_data(nested_attr));
 
787
                err = set_tcp(skb, flow_key, nla_data(a),
 
788
                              get_mask(a, struct ovs_key_tcp *));
538
789
                break;
539
790
 
540
791
        case OVS_KEY_ATTR_UDP:
541
 
                err = set_udp(skb, nla_data(nested_attr));
 
792
                err = set_udp(skb, flow_key, nla_data(a),
 
793
                              get_mask(a, struct ovs_key_udp *));
542
794
                break;
543
795
 
544
796
        case OVS_KEY_ATTR_SCTP:
545
 
                err = set_sctp(skb, nla_data(nested_attr));
 
797
                err = set_sctp(skb, flow_key, nla_data(a),
 
798
                               get_mask(a, struct ovs_key_sctp *));
 
799
                break;
 
800
 
 
801
        case OVS_KEY_ATTR_MPLS:
 
802
                err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
 
803
                                                                    __be32 *));
546
804
                break;
547
805
        }
548
806
 
550
808
}
551
809
 
552
810
static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
553
 
                                 const struct nlattr *a)
 
811
                          struct sw_flow_key *key,
 
812
                          const struct nlattr *a, int rem)
554
813
{
555
 
        struct sw_flow_key recirc_key;
556
 
        int err;
557
 
 
558
 
        err = ovs_flow_key_extract_recirc(nla_get_u32(a), OVS_CB(skb)->pkt_key,
559
 
                                          skb, &recirc_key);
560
 
        if (err) {
 
814
        struct deferred_action *da;
 
815
 
 
816
        if (!is_flow_key_valid(key)) {
 
817
                int err;
 
818
 
 
819
                err = ovs_flow_key_update(skb, key);
 
820
                if (err)
 
821
                        return err;
 
822
        }
 
823
        BUG_ON(!is_flow_key_valid(key));
 
824
 
 
825
        if (!nla_is_last(a, rem)) {
 
826
                /* Recirc action is the not the last action
 
827
                 * of the action list, need to clone the skb.
 
828
                 */
 
829
                skb = skb_clone(skb, GFP_ATOMIC);
 
830
 
 
831
                /* Skip the recirc action when out of memory, but
 
832
                 * continue on with the rest of the action list.
 
833
                 */
 
834
                if (!skb)
 
835
                        return 0;
 
836
        }
 
837
 
 
838
        da = add_deferred_actions(skb, key, NULL);
 
839
        if (da) {
 
840
                da->pkt_key.recirc_id = nla_get_u32(a);
 
841
        } else {
561
842
                kfree_skb(skb);
562
 
                return err;
 
843
 
 
844
                if (net_ratelimit())
 
845
                        pr_warn("%s: deferred action limit reached, drop recirc action\n",
 
846
                                ovs_dp_name(dp));
563
847
        }
564
848
 
565
 
 
566
 
        ovs_dp_process_packet_with_key(skb, &recirc_key, true);
567
 
 
568
849
        return 0;
569
850
}
570
851
 
571
852
/* Execute a list of actions against 'skb'. */
572
853
static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
573
 
                        const struct nlattr *attr, int len)
 
854
                              struct sw_flow_key *key,
 
855
                              const struct nlattr *attr, int len)
574
856
{
575
857
        /* Every output action needs a separate clone of 'skb', but the common
576
858
         * case is just a single output action, so that doing a clone and
577
859
         * then freeing the original skbuff is wasteful.  So the following code
578
 
         * is slightly obscure just to avoid that. */
 
860
         * is slightly obscure just to avoid that.
 
861
         */
579
862
        int prev_port = -1;
580
863
        const struct nlattr *a;
581
864
        int rem;
584
867
             a = nla_next(a, &rem)) {
585
868
                int err = 0;
586
869
 
587
 
                if (prev_port != -1) {
588
 
                        do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
 
870
                if (unlikely(prev_port != -1)) {
 
871
                        struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
 
872
 
 
873
                        if (out_skb)
 
874
                                do_output(dp, out_skb, prev_port);
 
875
 
589
876
                        prev_port = -1;
590
877
                }
591
878
 
595
882
                        break;
596
883
 
597
884
                case OVS_ACTION_ATTR_USERSPACE:
598
 
                        output_userspace(dp, skb, a);
 
885
                        output_userspace(dp, skb, key, a);
599
886
                        break;
600
887
 
601
888
                case OVS_ACTION_ATTR_HASH:
602
 
                        execute_hash(skb, a);
 
889
                        execute_hash(skb, key, a);
 
890
                        break;
 
891
 
 
892
                case OVS_ACTION_ATTR_PUSH_MPLS:
 
893
                        err = push_mpls(skb, key, nla_data(a));
 
894
                        break;
 
895
 
 
896
                case OVS_ACTION_ATTR_POP_MPLS:
 
897
                        err = pop_mpls(skb, key, nla_get_be16(a));
603
898
                        break;
604
899
 
605
900
                case OVS_ACTION_ATTR_PUSH_VLAN:
606
 
                        err = push_vlan(skb, nla_data(a));
607
 
                        if (unlikely(err)) /* skb already freed. */
 
901
                        err = push_vlan(skb, key, nla_data(a));
 
902
                        break;
 
903
 
 
904
                case OVS_ACTION_ATTR_POP_VLAN:
 
905
                        err = pop_vlan(skb, key);
 
906
                        break;
 
907
 
 
908
                case OVS_ACTION_ATTR_RECIRC:
 
909
                        err = execute_recirc(dp, skb, key, a, rem);
 
910
                        if (nla_is_last(a, rem)) {
 
911
                                /* If this is the last action, the skb has
 
912
                                 * been consumed or freed.
 
913
                                 * Return immediately.
 
914
                                 */
608
915
                                return err;
609
 
                        break;
610
 
 
611
 
                case OVS_ACTION_ATTR_POP_VLAN:
612
 
                        err = pop_vlan(skb);
613
 
                        break;
614
 
 
615
 
                case OVS_ACTION_ATTR_RECIRC: {
616
 
                        struct sk_buff *recirc_skb;
617
 
 
618
 
                        if (last_action(a, rem))
619
 
                                return execute_recirc(dp, skb, a);
620
 
 
621
 
                        /* Recirc action is the not the last action
622
 
                         * of the action list. */
623
 
                        recirc_skb = skb_clone(skb, GFP_ATOMIC);
624
 
 
625
 
                        /* Skip the recirc action when out of memory, but
626
 
                         * continue on with the rest of the action list. */
627
 
                        if (recirc_skb)
628
 
                                err = execute_recirc(dp, recirc_skb, a);
629
 
 
630
 
                        break;
631
 
                }
 
916
                        }
 
917
                        break;
632
918
 
633
919
                case OVS_ACTION_ATTR_SET:
634
 
                        err = execute_set_action(skb, nla_data(a));
 
920
                        err = execute_set_action(skb, key, nla_data(a));
 
921
                        break;
 
922
 
 
923
                case OVS_ACTION_ATTR_SET_MASKED:
 
924
                case OVS_ACTION_ATTR_SET_TO_MASKED:
 
925
                        err = execute_masked_set_action(skb, key, nla_data(a));
635
926
                        break;
636
927
 
637
928
                case OVS_ACTION_ATTR_SAMPLE:
638
 
                        err = sample(dp, skb, a);
 
929
                        err = sample(dp, skb, key, a);
639
930
                        break;
640
931
                }
641
932
 
653
944
        return 0;
654
945
}
655
946
 
656
 
/* We limit the number of times that we pass into execute_actions()
657
 
 * to avoid blowing out the stack in the event that we have a loop.
658
 
 *
659
 
 * Each loop adds some (estimated) cost to the kernel stack.
660
 
 * The loop terminates when the max cost is exceeded.
661
 
 * */
662
 
#define RECIRC_STACK_COST 1
663
 
#define DEFAULT_STACK_COST 4
664
 
/* Allow up to 4 regular services, and up to 3 recirculations */
665
 
#define MAX_STACK_COST (DEFAULT_STACK_COST * 4 + RECIRC_STACK_COST * 3)
666
 
 
667
 
struct loop_counter {
668
 
        u8 stack_cost;          /* loop stack cost. */
669
 
        bool looping;           /* Loop detected? */
670
 
};
671
 
 
672
 
static DEFINE_PER_CPU(struct loop_counter, loop_counters);
673
 
 
674
 
static int loop_suppress(struct datapath *dp, struct sw_flow_actions *actions)
675
 
{
676
 
        if (net_ratelimit())
677
 
                pr_warn("%s: flow loop detected, dropping\n",
 
947
static void process_deferred_actions(struct datapath *dp)
 
948
{
 
949
        struct action_fifo *fifo = this_cpu_ptr(action_fifos);
 
950
 
 
951
        /* Do not touch the FIFO in case there is no deferred actions. */
 
952
        if (action_fifo_is_empty(fifo))
 
953
                return;
 
954
 
 
955
        /* Finishing executing all deferred actions. */
 
956
        do {
 
957
                struct deferred_action *da = action_fifo_get(fifo);
 
958
                struct sk_buff *skb = da->skb;
 
959
                struct sw_flow_key *key = &da->pkt_key;
 
960
                const struct nlattr *actions = da->actions;
 
961
 
 
962
                if (actions)
 
963
                        do_execute_actions(dp, skb, key, actions,
 
964
                                           nla_len(actions));
 
965
                else
 
966
                        ovs_dp_process_packet(skb, key);
 
967
        } while (!action_fifo_is_empty(fifo));
 
968
 
 
969
        /* Reset FIFO for the next packet.  */
 
970
        action_fifo_init(fifo);
 
971
}
 
972
 
 
973
/* Execute a list of actions against 'skb'. */
 
974
int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
 
975
                        const struct sw_flow_actions *acts,
 
976
                        struct sw_flow_key *key)
 
977
{
 
978
        int level = this_cpu_read(exec_actions_level);
 
979
        int err;
 
980
 
 
981
        if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
 
982
                if (net_ratelimit())
 
983
                        pr_warn("%s: packet loop detected, dropping.\n",
678
984
                                ovs_dp_name(dp));
679
 
        actions->actions_len = 0;
680
 
        return -ELOOP;
681
 
}
682
 
 
683
 
/* Execute a list of actions against 'skb'. */
684
 
int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, bool recirc)
685
 
{
686
 
        struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
687
 
        const u8 stack_cost = recirc ? RECIRC_STACK_COST : DEFAULT_STACK_COST;
688
 
        struct loop_counter *loop;
689
 
        int error;
690
 
 
691
 
        /* Check whether we've looped too much. */
692
 
        loop = &__get_cpu_var(loop_counters);
693
 
        loop->stack_cost += stack_cost;
694
 
        if (unlikely(loop->stack_cost > MAX_STACK_COST))
695
 
                loop->looping = true;
696
 
        if (unlikely(loop->looping)) {
697
 
                error = loop_suppress(dp, acts);
 
985
 
698
986
                kfree_skb(skb);
699
 
                goto out_loop;
 
987
                return -ELOOP;
700
988
        }
701
989
 
702
 
        OVS_CB(skb)->tun_key = NULL;
703
 
        error = do_execute_actions(dp, skb, acts->actions, acts->actions_len);
704
 
 
705
 
        /* Check whether sub-actions looped too much. */
706
 
        if (unlikely(loop->looping))
707
 
                error = loop_suppress(dp, acts);
708
 
 
709
 
out_loop:
710
 
        /* Decrement loop stack cost. */
711
 
        loop->stack_cost -= stack_cost;
712
 
        if (!loop->stack_cost)
713
 
                loop->looping = false;
714
 
 
715
 
        return error;
 
990
        this_cpu_inc(exec_actions_level);
 
991
        err = do_execute_actions(dp, skb, key,
 
992
                                 acts->actions, acts->actions_len);
 
993
 
 
994
        if (!level)
 
995
                process_deferred_actions(dp);
 
996
 
 
997
        this_cpu_dec(exec_actions_level);
 
998
 
 
999
        /* This return status currently does not reflect the errors
 
1000
         * encounted during deferred actions execution. Probably needs to
 
1001
         * be fixed in the future.
 
1002
         */
 
1003
        return err;
 
1004
}
 
1005
 
 
1006
int action_fifos_init(void)
 
1007
{
 
1008
        action_fifos = alloc_percpu(struct action_fifo);
 
1009
        if (!action_fifos)
 
1010
                return -ENOMEM;
 
1011
 
 
1012
        return 0;
 
1013
}
 
1014
 
 
1015
void action_fifos_exit(void)
 
1016
{
 
1017
        free_percpu(action_fifos);
716
1018
}