Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1 | /* |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301, USA |
| 17 | */ |
| 18 | |
| 19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
| 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/in.h> |
| 23 | #include <linux/ip.h> |
| 24 | #include <linux/openvswitch.h> |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 25 | #include <linux/sctp.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 26 | #include <linux/tcp.h> |
| 27 | #include <linux/udp.h> |
| 28 | #include <linux/in6.h> |
| 29 | #include <linux/if_arp.h> |
| 30 | #include <linux/if_vlan.h> |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 31 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 32 | #include <net/ip.h> |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 33 | #include <net/ipv6.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 34 | #include <net/checksum.h> |
| 35 | #include <net/dsfield.h> |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 36 | #include <net/mpls.h> |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 37 | #include <net/sctp/checksum.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 38 | |
| 39 | #include "datapath.h" |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 40 | #include "flow.h" |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 41 | #include "vport.h" |
| 42 | |
| 43 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame] | 44 | struct sw_flow_key *key, |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 45 | const struct nlattr *attr, int len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 46 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 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 | static DEFINE_PER_CPU(int, exec_actions_level); |
| 65 | |
| 66 | static void action_fifo_init(struct action_fifo *fifo) |
| 67 | { |
| 68 | fifo->head = 0; |
| 69 | fifo->tail = 0; |
| 70 | } |
| 71 | |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 72 | static bool action_fifo_is_empty(const struct action_fifo *fifo) |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 73 | { |
| 74 | return (fifo->head == fifo->tail); |
| 75 | } |
| 76 | |
| 77 | static struct deferred_action *action_fifo_get(struct action_fifo *fifo) |
| 78 | { |
| 79 | if (action_fifo_is_empty(fifo)) |
| 80 | return NULL; |
| 81 | |
| 82 | return &fifo->fifo[fifo->tail++]; |
| 83 | } |
| 84 | |
| 85 | static struct deferred_action *action_fifo_put(struct action_fifo *fifo) |
| 86 | { |
| 87 | if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1) |
| 88 | return NULL; |
| 89 | |
| 90 | return &fifo->fifo[fifo->head++]; |
| 91 | } |
| 92 | |
| 93 | /* Return true if fifo is not full */ |
| 94 | static struct deferred_action *add_deferred_actions(struct sk_buff *skb, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 95 | const struct sw_flow_key *key, |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 96 | const struct nlattr *attr) |
| 97 | { |
| 98 | struct action_fifo *fifo; |
| 99 | struct deferred_action *da; |
| 100 | |
| 101 | fifo = this_cpu_ptr(action_fifos); |
| 102 | da = action_fifo_put(fifo); |
| 103 | if (da) { |
| 104 | da->skb = skb; |
| 105 | da->actions = attr; |
| 106 | da->pkt_key = *key; |
| 107 | } |
| 108 | |
| 109 | return da; |
| 110 | } |
| 111 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 112 | static void invalidate_flow_key(struct sw_flow_key *key) |
| 113 | { |
| 114 | key->eth.type = htons(0); |
| 115 | } |
| 116 | |
| 117 | static bool is_flow_key_valid(const struct sw_flow_key *key) |
| 118 | { |
| 119 | return !!key->eth.type; |
| 120 | } |
| 121 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 122 | static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key, |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 123 | const struct ovs_action_push_mpls *mpls) |
| 124 | { |
| 125 | __be32 *new_mpls_lse; |
| 126 | struct ethhdr *hdr; |
| 127 | |
| 128 | /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */ |
| 129 | if (skb->encapsulation) |
| 130 | return -ENOTSUPP; |
| 131 | |
| 132 | if (skb_cow_head(skb, MPLS_HLEN) < 0) |
| 133 | return -ENOMEM; |
| 134 | |
| 135 | skb_push(skb, MPLS_HLEN); |
| 136 | memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), |
| 137 | skb->mac_len); |
| 138 | skb_reset_mac_header(skb); |
| 139 | |
| 140 | new_mpls_lse = (__be32 *)skb_mpls_header(skb); |
| 141 | *new_mpls_lse = mpls->mpls_lse; |
| 142 | |
| 143 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 144 | skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse, |
| 145 | MPLS_HLEN, 0)); |
| 146 | |
| 147 | hdr = eth_hdr(skb); |
| 148 | hdr->h_proto = mpls->mpls_ethertype; |
| 149 | |
Pravin B Shelar | cbe7e76 | 2014-12-23 16:20:28 -0800 | [diff] [blame] | 150 | if (!skb->inner_protocol) |
| 151 | skb_set_inner_protocol(skb, skb->protocol); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 152 | skb->protocol = mpls->mpls_ethertype; |
| 153 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 154 | invalidate_flow_key(key); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 158 | static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key, |
| 159 | const __be16 ethertype) |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 160 | { |
| 161 | struct ethhdr *hdr; |
| 162 | int err; |
| 163 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 164 | err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 165 | if (unlikely(err)) |
| 166 | return err; |
| 167 | |
Jiri Pirko | 1abcd82 | 2014-11-19 14:04:55 +0100 | [diff] [blame] | 168 | skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 169 | |
| 170 | memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), |
| 171 | skb->mac_len); |
| 172 | |
| 173 | __skb_pull(skb, MPLS_HLEN); |
| 174 | skb_reset_mac_header(skb); |
| 175 | |
| 176 | /* skb_mpls_header() is used to locate the ethertype |
| 177 | * field correctly in the presence of VLAN tags. |
| 178 | */ |
| 179 | hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN); |
| 180 | hdr->h_proto = ethertype; |
| 181 | if (eth_p_mpls(skb->protocol)) |
| 182 | skb->protocol = ethertype; |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 183 | |
| 184 | invalidate_flow_key(key); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 188 | static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 189 | const __be32 *mpls_lse, const __be32 *mask) |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 190 | { |
| 191 | __be32 *stack; |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 192 | __be32 lse; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 193 | int err; |
| 194 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 195 | err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 196 | if (unlikely(err)) |
| 197 | return err; |
| 198 | |
| 199 | stack = (__be32 *)skb_mpls_header(skb); |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 200 | lse = OVS_MASKED(*stack, *mpls_lse, *mask); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 201 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 202 | __be32 diff[] = { ~(*stack), lse }; |
| 203 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 204 | skb->csum = ~csum_partial((char *)diff, sizeof(diff), |
| 205 | ~skb->csum); |
| 206 | } |
| 207 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 208 | *stack = lse; |
| 209 | flow_key->mpls.top_lse = lse; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 213 | static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 214 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 215 | int err; |
| 216 | |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 217 | err = skb_vlan_pop(skb); |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 218 | if (skb_vlan_tag_present(skb)) |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 219 | invalidate_flow_key(key); |
| 220 | else |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 221 | key->eth.tci = 0; |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 222 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 225 | static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key, |
| 226 | const struct ovs_action_push_vlan *vlan) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 227 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 228 | if (skb_vlan_tag_present(skb)) |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 229 | invalidate_flow_key(key); |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 230 | else |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 231 | key->eth.tci = vlan->vlan_tci; |
Jiri Pirko | 93515d5 | 2014-11-19 14:05:02 +0100 | [diff] [blame] | 232 | return skb_vlan_push(skb, vlan->vlan_tpid, |
| 233 | ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 236 | /* 'src' is already properly masked. */ |
| 237 | static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_) |
| 238 | { |
| 239 | u16 *dst = (u16 *)dst_; |
| 240 | const u16 *src = (const u16 *)src_; |
| 241 | const u16 *mask = (const u16 *)mask_; |
| 242 | |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 243 | OVS_SET_MASKED(dst[0], src[0], mask[0]); |
| 244 | OVS_SET_MASKED(dst[1], src[1], mask[1]); |
| 245 | OVS_SET_MASKED(dst[2], src[2], mask[2]); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 249 | const struct ovs_key_ethernet *key, |
| 250 | const struct ovs_key_ethernet *mask) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 251 | { |
| 252 | int err; |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 253 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 254 | err = skb_ensure_writable(skb, ETH_HLEN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 255 | if (unlikely(err)) |
| 256 | return err; |
| 257 | |
Pravin B Shelar | b34df5e | 2013-06-13 11:11:44 -0700 | [diff] [blame] | 258 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
| 259 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 260 | ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src, |
| 261 | mask->eth_src); |
| 262 | ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst, |
| 263 | mask->eth_dst); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 264 | |
Pravin B Shelar | b34df5e | 2013-06-13 11:11:44 -0700 | [diff] [blame] | 265 | ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
| 266 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 267 | ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source); |
| 268 | ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Glenn Griffin | 3576fd7 | 2015-08-03 09:56:54 -0700 | [diff] [blame] | 272 | static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh, |
| 273 | __be32 addr, __be32 new_addr) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 274 | { |
| 275 | int transport_len = skb->len - skb_transport_offset(skb); |
| 276 | |
Glenn Griffin | 3576fd7 | 2015-08-03 09:56:54 -0700 | [diff] [blame] | 277 | if (nh->frag_off & htons(IP_OFFSET)) |
| 278 | return; |
| 279 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 280 | if (nh->protocol == IPPROTO_TCP) { |
| 281 | if (likely(transport_len >= sizeof(struct tcphdr))) |
| 282 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 283 | addr, new_addr, true); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 284 | } else if (nh->protocol == IPPROTO_UDP) { |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 285 | if (likely(transport_len >= sizeof(struct udphdr))) { |
| 286 | struct udphdr *uh = udp_hdr(skb); |
| 287 | |
| 288 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
| 289 | inet_proto_csum_replace4(&uh->check, skb, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 290 | addr, new_addr, true); |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 291 | if (!uh->check) |
| 292 | uh->check = CSUM_MANGLED_0; |
| 293 | } |
| 294 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 295 | } |
Glenn Griffin | 3576fd7 | 2015-08-03 09:56:54 -0700 | [diff] [blame] | 296 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 297 | |
Glenn Griffin | 3576fd7 | 2015-08-03 09:56:54 -0700 | [diff] [blame] | 298 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
| 299 | __be32 *addr, __be32 new_addr) |
| 300 | { |
| 301 | update_ip_l4_checksum(skb, nh, *addr, new_addr); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 302 | csum_replace4(&nh->check, *addr, new_addr); |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 303 | skb_clear_hash(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 304 | *addr = new_addr; |
| 305 | } |
| 306 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 307 | static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, |
| 308 | __be32 addr[4], const __be32 new_addr[4]) |
| 309 | { |
| 310 | int transport_len = skb->len - skb_transport_offset(skb); |
| 311 | |
Jesse Gross | 856447d | 2014-11-11 14:32:20 -0800 | [diff] [blame] | 312 | if (l4_proto == NEXTHDR_TCP) { |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 313 | if (likely(transport_len >= sizeof(struct tcphdr))) |
| 314 | inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 315 | addr, new_addr, true); |
Jesse Gross | 856447d | 2014-11-11 14:32:20 -0800 | [diff] [blame] | 316 | } else if (l4_proto == NEXTHDR_UDP) { |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 317 | if (likely(transport_len >= sizeof(struct udphdr))) { |
| 318 | struct udphdr *uh = udp_hdr(skb); |
| 319 | |
| 320 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
| 321 | inet_proto_csum_replace16(&uh->check, skb, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 322 | addr, new_addr, true); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 323 | if (!uh->check) |
| 324 | uh->check = CSUM_MANGLED_0; |
| 325 | } |
| 326 | } |
Jesse Gross | 856447d | 2014-11-11 14:32:20 -0800 | [diff] [blame] | 327 | } else if (l4_proto == NEXTHDR_ICMP) { |
| 328 | if (likely(transport_len >= sizeof(struct icmp6hdr))) |
| 329 | inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum, |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 330 | skb, addr, new_addr, true); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 334 | static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4], |
| 335 | const __be32 mask[4], __be32 masked[4]) |
| 336 | { |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 337 | masked[0] = OVS_MASKED(old[0], addr[0], mask[0]); |
| 338 | masked[1] = OVS_MASKED(old[1], addr[1], mask[1]); |
| 339 | masked[2] = OVS_MASKED(old[2], addr[2], mask[2]); |
| 340 | masked[3] = OVS_MASKED(old[3], addr[3], mask[3]); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 343 | static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, |
| 344 | __be32 addr[4], const __be32 new_addr[4], |
| 345 | bool recalculate_csum) |
| 346 | { |
| 347 | if (recalculate_csum) |
| 348 | update_ipv6_checksum(skb, l4_proto, addr, new_addr); |
| 349 | |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 350 | skb_clear_hash(skb); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 351 | memcpy(addr, new_addr, sizeof(__be32[4])); |
| 352 | } |
| 353 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 354 | static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask) |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 355 | { |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 356 | /* Bits 21-24 are always unmasked, so this retains their values. */ |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 357 | OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16)); |
| 358 | OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8)); |
| 359 | OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 362 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl, |
| 363 | u8 mask) |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 364 | { |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 365 | new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 366 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 367 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); |
| 368 | nh->ttl = new_ttl; |
| 369 | } |
| 370 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 371 | static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 372 | const struct ovs_key_ipv4 *key, |
| 373 | const struct ovs_key_ipv4 *mask) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 374 | { |
| 375 | struct iphdr *nh; |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 376 | __be32 new_addr; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 377 | int err; |
| 378 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 379 | err = skb_ensure_writable(skb, skb_network_offset(skb) + |
| 380 | sizeof(struct iphdr)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 381 | if (unlikely(err)) |
| 382 | return err; |
| 383 | |
| 384 | nh = ip_hdr(skb); |
| 385 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 386 | /* Setting an IP addresses is typically only a side effect of |
| 387 | * matching on them in the current userspace implementation, so it |
| 388 | * makes sense to check if the value actually changed. |
| 389 | */ |
| 390 | if (mask->ipv4_src) { |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 391 | new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 392 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 393 | if (unlikely(new_addr != nh->saddr)) { |
| 394 | set_ip_addr(skb, nh, &nh->saddr, new_addr); |
| 395 | flow_key->ipv4.addr.src = new_addr; |
| 396 | } |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 397 | } |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 398 | if (mask->ipv4_dst) { |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 399 | new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 400 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 401 | if (unlikely(new_addr != nh->daddr)) { |
| 402 | set_ip_addr(skb, nh, &nh->daddr, new_addr); |
| 403 | flow_key->ipv4.addr.dst = new_addr; |
| 404 | } |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 405 | } |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 406 | if (mask->ipv4_tos) { |
| 407 | ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos); |
| 408 | flow_key->ip.tos = nh->tos; |
| 409 | } |
| 410 | if (mask->ipv4_ttl) { |
| 411 | set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl); |
| 412 | flow_key->ip.ttl = nh->ttl; |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 413 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 418 | static bool is_ipv6_mask_nonzero(const __be32 addr[4]) |
| 419 | { |
| 420 | return !!(addr[0] | addr[1] | addr[2] | addr[3]); |
| 421 | } |
| 422 | |
| 423 | static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 424 | const struct ovs_key_ipv6 *key, |
| 425 | const struct ovs_key_ipv6 *mask) |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 426 | { |
| 427 | struct ipv6hdr *nh; |
| 428 | int err; |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 429 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 430 | err = skb_ensure_writable(skb, skb_network_offset(skb) + |
| 431 | sizeof(struct ipv6hdr)); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 432 | if (unlikely(err)) |
| 433 | return err; |
| 434 | |
| 435 | nh = ipv6_hdr(skb); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 436 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 437 | /* Setting an IP addresses is typically only a side effect of |
| 438 | * matching on them in the current userspace implementation, so it |
| 439 | * makes sense to check if the value actually changed. |
| 440 | */ |
| 441 | if (is_ipv6_mask_nonzero(mask->ipv6_src)) { |
| 442 | __be32 *saddr = (__be32 *)&nh->saddr; |
| 443 | __be32 masked[4]; |
| 444 | |
| 445 | mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked); |
| 446 | |
| 447 | if (unlikely(memcmp(saddr, masked, sizeof(masked)))) { |
| 448 | set_ipv6_addr(skb, key->ipv6_proto, saddr, masked, |
| 449 | true); |
| 450 | memcpy(&flow_key->ipv6.addr.src, masked, |
| 451 | sizeof(flow_key->ipv6.addr.src)); |
| 452 | } |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 453 | } |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 454 | if (is_ipv6_mask_nonzero(mask->ipv6_dst)) { |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 455 | unsigned int offset = 0; |
| 456 | int flags = IP6_FH_F_SKIP_RH; |
| 457 | bool recalc_csum = true; |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 458 | __be32 *daddr = (__be32 *)&nh->daddr; |
| 459 | __be32 masked[4]; |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 460 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 461 | mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 462 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 463 | if (unlikely(memcmp(daddr, masked, sizeof(masked)))) { |
| 464 | if (ipv6_ext_hdr(nh->nexthdr)) |
| 465 | recalc_csum = (ipv6_find_hdr(skb, &offset, |
| 466 | NEXTHDR_ROUTING, |
| 467 | NULL, &flags) |
| 468 | != NEXTHDR_ROUTING); |
| 469 | |
| 470 | set_ipv6_addr(skb, key->ipv6_proto, daddr, masked, |
| 471 | recalc_csum); |
| 472 | memcpy(&flow_key->ipv6.addr.dst, masked, |
| 473 | sizeof(flow_key->ipv6.addr.dst)); |
| 474 | } |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 475 | } |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 476 | if (mask->ipv6_tclass) { |
| 477 | ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass); |
| 478 | flow_key->ip.tos = ipv6_get_dsfield(nh); |
| 479 | } |
| 480 | if (mask->ipv6_label) { |
| 481 | set_ipv6_fl(nh, ntohl(key->ipv6_label), |
| 482 | ntohl(mask->ipv6_label)); |
| 483 | flow_key->ipv6.label = |
| 484 | *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); |
| 485 | } |
| 486 | if (mask->ipv6_hlimit) { |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 487 | OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit, |
| 488 | mask->ipv6_hlimit); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 489 | flow_key->ip.ttl = nh->hop_limit; |
| 490 | } |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 494 | /* Must follow skb_ensure_writable() since that can move the skb data. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 495 | static void set_tp_port(struct sk_buff *skb, __be16 *port, |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 496 | __be16 new_port, __sum16 *check) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 497 | { |
Tom Herbert | 4b048d6 | 2015-08-17 13:42:25 -0700 | [diff] [blame] | 498 | inet_proto_csum_replace2(check, skb, *port, new_port, false); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 499 | *port = new_port; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 502 | static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 503 | const struct ovs_key_udp *key, |
| 504 | const struct ovs_key_udp *mask) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 505 | { |
| 506 | struct udphdr *uh; |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 507 | __be16 src, dst; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 508 | int err; |
| 509 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 510 | err = skb_ensure_writable(skb, skb_transport_offset(skb) + |
| 511 | sizeof(struct udphdr)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 512 | if (unlikely(err)) |
| 513 | return err; |
| 514 | |
| 515 | uh = udp_hdr(skb); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 516 | /* Either of the masks is non-zero, so do not bother checking them. */ |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 517 | src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src); |
| 518 | dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 519 | |
| 520 | if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) { |
| 521 | if (likely(src != uh->source)) { |
| 522 | set_tp_port(skb, &uh->source, src, &uh->check); |
| 523 | flow_key->tp.src = src; |
| 524 | } |
| 525 | if (likely(dst != uh->dest)) { |
| 526 | set_tp_port(skb, &uh->dest, dst, &uh->check); |
| 527 | flow_key->tp.dst = dst; |
| 528 | } |
| 529 | |
| 530 | if (unlikely(!uh->check)) |
| 531 | uh->check = CSUM_MANGLED_0; |
| 532 | } else { |
| 533 | uh->source = src; |
| 534 | uh->dest = dst; |
| 535 | flow_key->tp.src = src; |
| 536 | flow_key->tp.dst = dst; |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 537 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 538 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 539 | skb_clear_hash(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 544 | static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 545 | const struct ovs_key_tcp *key, |
| 546 | const struct ovs_key_tcp *mask) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 547 | { |
| 548 | struct tcphdr *th; |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 549 | __be16 src, dst; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 550 | int err; |
| 551 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 552 | err = skb_ensure_writable(skb, skb_transport_offset(skb) + |
| 553 | sizeof(struct tcphdr)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 554 | if (unlikely(err)) |
| 555 | return err; |
| 556 | |
| 557 | th = tcp_hdr(skb); |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 558 | src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 559 | if (likely(src != th->source)) { |
| 560 | set_tp_port(skb, &th->source, src, &th->check); |
| 561 | flow_key->tp.src = src; |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 562 | } |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 563 | dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 564 | if (likely(dst != th->dest)) { |
| 565 | set_tp_port(skb, &th->dest, dst, &th->check); |
| 566 | flow_key->tp.dst = dst; |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 567 | } |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 568 | skb_clear_hash(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 573 | static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key, |
| 574 | const struct ovs_key_sctp *key, |
| 575 | const struct ovs_key_sctp *mask) |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 576 | { |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 577 | unsigned int sctphoff = skb_transport_offset(skb); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 578 | struct sctphdr *sh; |
| 579 | __le32 old_correct_csum, new_csum, old_csum; |
| 580 | int err; |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 581 | |
Jiri Pirko | e219512 | 2014-11-19 14:05:01 +0100 | [diff] [blame] | 582 | err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr)); |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 583 | if (unlikely(err)) |
| 584 | return err; |
| 585 | |
| 586 | sh = sctp_hdr(skb); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 587 | old_csum = sh->checksum; |
| 588 | old_correct_csum = sctp_compute_cksum(skb, sctphoff); |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 589 | |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 590 | sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src); |
| 591 | sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst); |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 592 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 593 | new_csum = sctp_compute_cksum(skb, sctphoff); |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 594 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 595 | /* Carry any checksum errors through. */ |
| 596 | sh->checksum = old_csum ^ old_correct_csum ^ new_csum; |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 597 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 598 | skb_clear_hash(skb); |
| 599 | flow_key->tp.src = sh->source; |
| 600 | flow_key->tp.dst = sh->dest; |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Andy Zhou | 738967b | 2014-09-08 00:35:02 -0700 | [diff] [blame] | 605 | static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 606 | { |
Andy Zhou | 738967b | 2014-09-08 00:35:02 -0700 | [diff] [blame] | 607 | struct vport *vport = ovs_vport_rcu(dp, out_port); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 608 | |
Andy Zhou | 738967b | 2014-09-08 00:35:02 -0700 | [diff] [blame] | 609 | if (likely(vport)) |
| 610 | ovs_vport_send(vport, skb); |
| 611 | else |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 612 | kfree_skb(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 616 | struct sw_flow_key *key, const struct nlattr *attr, |
| 617 | const struct nlattr *actions, int actions_len) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 618 | { |
Thomas Graf | 1d8fff9 | 2015-07-21 10:43:54 +0200 | [diff] [blame] | 619 | struct ip_tunnel_info info; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 620 | struct dp_upcall_info upcall; |
| 621 | const struct nlattr *a; |
| 622 | int rem; |
| 623 | |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 624 | memset(&upcall, 0, sizeof(upcall)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 625 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 626 | |
| 627 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 628 | a = nla_next(a, &rem)) { |
| 629 | switch (nla_type(a)) { |
| 630 | case OVS_USERSPACE_ATTR_USERDATA: |
| 631 | upcall.userdata = a; |
| 632 | break; |
| 633 | |
| 634 | case OVS_USERSPACE_ATTR_PID: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 635 | upcall.portid = nla_get_u32(a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 636 | break; |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 637 | |
| 638 | case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: { |
| 639 | /* Get out tunnel info. */ |
| 640 | struct vport *vport; |
| 641 | |
| 642 | vport = ovs_vport_rcu(dp, nla_get_u32(a)); |
| 643 | if (vport) { |
| 644 | int err; |
| 645 | |
| 646 | err = ovs_vport_get_egress_tun_info(vport, skb, |
| 647 | &info); |
| 648 | if (!err) |
| 649 | upcall.egress_tun_info = &info; |
| 650 | } |
| 651 | break; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 652 | } |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 653 | |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 654 | case OVS_USERSPACE_ATTR_ACTIONS: { |
| 655 | /* Include actions. */ |
| 656 | upcall.actions = actions; |
| 657 | upcall.actions_len = actions_len; |
| 658 | break; |
| 659 | } |
| 660 | |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 661 | } /* End of switch. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Pravin B Shelar | e8eedb8 | 2014-11-06 06:57:27 -0800 | [diff] [blame] | 664 | return ovs_dp_upcall(dp, skb, key, &upcall); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static int sample(struct datapath *dp, struct sk_buff *skb, |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 668 | struct sw_flow_key *key, const struct nlattr *attr, |
| 669 | const struct nlattr *actions, int actions_len) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 670 | { |
| 671 | const struct nlattr *acts_list = NULL; |
| 672 | const struct nlattr *a; |
| 673 | int rem; |
| 674 | |
| 675 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 676 | a = nla_next(a, &rem)) { |
Wenyu Zhang | e05176a | 2015-08-05 00:30:47 -0700 | [diff] [blame] | 677 | u32 probability; |
| 678 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 679 | switch (nla_type(a)) { |
| 680 | case OVS_SAMPLE_ATTR_PROBABILITY: |
Wenyu Zhang | e05176a | 2015-08-05 00:30:47 -0700 | [diff] [blame] | 681 | probability = nla_get_u32(a); |
| 682 | if (!probability || prandom_u32() > probability) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 683 | return 0; |
| 684 | break; |
| 685 | |
| 686 | case OVS_SAMPLE_ATTR_ACTIONS: |
| 687 | acts_list = a; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 692 | rem = nla_len(acts_list); |
| 693 | a = nla_data(acts_list); |
| 694 | |
Andy Zhou | 32ae87f | 2014-09-15 19:33:50 -0700 | [diff] [blame] | 695 | /* Actions list is empty, do nothing */ |
| 696 | if (unlikely(!rem)) |
| 697 | return 0; |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 698 | |
Andy Zhou | 32ae87f | 2014-09-15 19:33:50 -0700 | [diff] [blame] | 699 | /* The only known usage of sample action is having a single user-space |
| 700 | * action. Treat this usage as a special case. |
| 701 | * The output_userspace() should clone the skb to be sent to the |
| 702 | * user space. This skb will be consumed by its caller. |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 703 | */ |
Andy Zhou | 32ae87f | 2014-09-15 19:33:50 -0700 | [diff] [blame] | 704 | if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE && |
Simon Horman | 941d8eb | 2014-10-27 16:12:16 +0900 | [diff] [blame] | 705 | nla_is_last(a, rem))) |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 706 | return output_userspace(dp, skb, key, a, actions, actions_len); |
Andy Zhou | 32ae87f | 2014-09-15 19:33:50 -0700 | [diff] [blame] | 707 | |
| 708 | skb = skb_clone(skb, GFP_ATOMIC); |
| 709 | if (!skb) |
| 710 | /* Skip the sample action when out of memory. */ |
| 711 | return 0; |
| 712 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 713 | if (!add_deferred_actions(skb, key, a)) { |
| 714 | if (net_ratelimit()) |
| 715 | pr_warn("%s: deferred actions limit reached, dropping sample action\n", |
| 716 | ovs_dp_name(dp)); |
| 717 | |
| 718 | kfree_skb(skb); |
| 719 | } |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key, |
| 724 | const struct nlattr *attr) |
| 725 | { |
| 726 | struct ovs_action_hash *hash_act = nla_data(attr); |
| 727 | u32 hash = 0; |
| 728 | |
| 729 | /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */ |
| 730 | hash = skb_get_hash(skb); |
| 731 | hash = jhash_1word(hash, hash_act->hash_basis); |
| 732 | if (!hash) |
| 733 | hash = 0x1; |
| 734 | |
| 735 | key->ovs_flow_hash = hash; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 738 | static int execute_set_action(struct sk_buff *skb, |
| 739 | struct sw_flow_key *flow_key, |
| 740 | const struct nlattr *a) |
| 741 | { |
| 742 | /* Only tunnel set execution is supported without a mask. */ |
| 743 | if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) { |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 744 | struct ovs_tunnel_info *tun = nla_data(a); |
| 745 | |
| 746 | skb_dst_drop(skb); |
| 747 | dst_hold((struct dst_entry *)tun->tun_dst); |
| 748 | skb_dst_set(skb, (struct dst_entry *)tun->tun_dst); |
| 749 | |
| 750 | /* FIXME: Remove when all vports have been converted */ |
| 751 | OVS_CB(skb)->egress_tun_info = &tun->tun_dst->u.tun_info; |
| 752 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | |
| 759 | /* Mask is at the midpoint of the data. */ |
| 760 | #define get_mask(a, type) ((const type)nla_data(a) + 1) |
| 761 | |
| 762 | static int execute_masked_set_action(struct sk_buff *skb, |
| 763 | struct sw_flow_key *flow_key, |
| 764 | const struct nlattr *a) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 765 | { |
| 766 | int err = 0; |
| 767 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 768 | switch (nla_type(a)) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 769 | case OVS_KEY_ATTR_PRIORITY: |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 770 | OVS_SET_MASKED(skb->priority, nla_get_u32(a), |
| 771 | *get_mask(a, u32 *)); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 772 | flow_key->phy.priority = skb->priority; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 773 | break; |
| 774 | |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 775 | case OVS_KEY_ATTR_SKB_MARK: |
Joe Stringer | be26b9a | 2015-08-26 11:31:45 -0700 | [diff] [blame^] | 776 | OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *)); |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 777 | flow_key->phy.skb_mark = skb->mark; |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 778 | break; |
| 779 | |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 780 | case OVS_KEY_ATTR_TUNNEL_INFO: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 781 | /* Masked data not supported for tunnel. */ |
| 782 | err = -EINVAL; |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 783 | break; |
| 784 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 785 | case OVS_KEY_ATTR_ETHERNET: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 786 | err = set_eth_addr(skb, flow_key, nla_data(a), |
| 787 | get_mask(a, struct ovs_key_ethernet *)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 788 | break; |
| 789 | |
| 790 | case OVS_KEY_ATTR_IPV4: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 791 | err = set_ipv4(skb, flow_key, nla_data(a), |
| 792 | get_mask(a, struct ovs_key_ipv4 *)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 793 | break; |
| 794 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 795 | case OVS_KEY_ATTR_IPV6: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 796 | err = set_ipv6(skb, flow_key, nla_data(a), |
| 797 | get_mask(a, struct ovs_key_ipv6 *)); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 798 | break; |
| 799 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 800 | case OVS_KEY_ATTR_TCP: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 801 | err = set_tcp(skb, flow_key, nla_data(a), |
| 802 | get_mask(a, struct ovs_key_tcp *)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 803 | break; |
| 804 | |
| 805 | case OVS_KEY_ATTR_UDP: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 806 | err = set_udp(skb, flow_key, nla_data(a), |
| 807 | get_mask(a, struct ovs_key_udp *)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 808 | break; |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 809 | |
| 810 | case OVS_KEY_ATTR_SCTP: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 811 | err = set_sctp(skb, flow_key, nla_data(a), |
| 812 | get_mask(a, struct ovs_key_sctp *)); |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 813 | break; |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 814 | |
| 815 | case OVS_KEY_ATTR_MPLS: |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 816 | err = set_mpls(skb, flow_key, nla_data(a), get_mask(a, |
| 817 | __be32 *)); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 818 | break; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return err; |
| 822 | } |
| 823 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 824 | static int execute_recirc(struct datapath *dp, struct sk_buff *skb, |
| 825 | struct sw_flow_key *key, |
| 826 | const struct nlattr *a, int rem) |
| 827 | { |
| 828 | struct deferred_action *da; |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 829 | |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 830 | if (!is_flow_key_valid(key)) { |
| 831 | int err; |
| 832 | |
| 833 | err = ovs_flow_key_update(skb, key); |
| 834 | if (err) |
| 835 | return err; |
| 836 | } |
| 837 | BUG_ON(!is_flow_key_valid(key)); |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 838 | |
Simon Horman | 941d8eb | 2014-10-27 16:12:16 +0900 | [diff] [blame] | 839 | if (!nla_is_last(a, rem)) { |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 840 | /* Recirc action is the not the last action |
| 841 | * of the action list, need to clone the skb. |
| 842 | */ |
| 843 | skb = skb_clone(skb, GFP_ATOMIC); |
| 844 | |
| 845 | /* Skip the recirc action when out of memory, but |
| 846 | * continue on with the rest of the action list. |
| 847 | */ |
| 848 | if (!skb) |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | da = add_deferred_actions(skb, key, NULL); |
| 853 | if (da) { |
| 854 | da->pkt_key.recirc_id = nla_get_u32(a); |
| 855 | } else { |
| 856 | kfree_skb(skb); |
| 857 | |
| 858 | if (net_ratelimit()) |
| 859 | pr_warn("%s: deferred action limit reached, drop recirc action\n", |
| 860 | ovs_dp_name(dp)); |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 866 | /* Execute a list of actions against 'skb'. */ |
| 867 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame] | 868 | struct sw_flow_key *key, |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 869 | const struct nlattr *attr, int len) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 870 | { |
| 871 | /* Every output action needs a separate clone of 'skb', but the common |
| 872 | * case is just a single output action, so that doing a clone and |
| 873 | * then freeing the original skbuff is wasteful. So the following code |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 874 | * is slightly obscure just to avoid that. |
| 875 | */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 876 | int prev_port = -1; |
| 877 | const struct nlattr *a; |
| 878 | int rem; |
| 879 | |
| 880 | for (a = attr, rem = len; rem > 0; |
| 881 | a = nla_next(a, &rem)) { |
| 882 | int err = 0; |
| 883 | |
Andy Zhou | 738967b | 2014-09-08 00:35:02 -0700 | [diff] [blame] | 884 | if (unlikely(prev_port != -1)) { |
| 885 | struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC); |
| 886 | |
| 887 | if (out_skb) |
| 888 | do_output(dp, out_skb, prev_port); |
| 889 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 890 | prev_port = -1; |
| 891 | } |
| 892 | |
| 893 | switch (nla_type(a)) { |
| 894 | case OVS_ACTION_ATTR_OUTPUT: |
| 895 | prev_port = nla_get_u32(a); |
| 896 | break; |
| 897 | |
| 898 | case OVS_ACTION_ATTR_USERSPACE: |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 899 | output_userspace(dp, skb, key, a, attr, len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 900 | break; |
| 901 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 902 | case OVS_ACTION_ATTR_HASH: |
| 903 | execute_hash(skb, key, a); |
| 904 | break; |
| 905 | |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 906 | case OVS_ACTION_ATTR_PUSH_MPLS: |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 907 | err = push_mpls(skb, key, nla_data(a)); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 908 | break; |
| 909 | |
| 910 | case OVS_ACTION_ATTR_POP_MPLS: |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 911 | err = pop_mpls(skb, key, nla_get_be16(a)); |
Simon Horman | 25cd9ba | 2014-10-06 05:05:13 -0700 | [diff] [blame] | 912 | break; |
| 913 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 914 | case OVS_ACTION_ATTR_PUSH_VLAN: |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 915 | err = push_vlan(skb, key, nla_data(a)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 916 | break; |
| 917 | |
| 918 | case OVS_ACTION_ATTR_POP_VLAN: |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 919 | err = pop_vlan(skb, key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 920 | break; |
| 921 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 922 | case OVS_ACTION_ATTR_RECIRC: |
| 923 | err = execute_recirc(dp, skb, key, a, rem); |
Simon Horman | 941d8eb | 2014-10-27 16:12:16 +0900 | [diff] [blame] | 924 | if (nla_is_last(a, rem)) { |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 925 | /* If this is the last action, the skb has |
| 926 | * been consumed or freed. |
| 927 | * Return immediately. |
| 928 | */ |
| 929 | return err; |
| 930 | } |
| 931 | break; |
| 932 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 933 | case OVS_ACTION_ATTR_SET: |
Pravin B Shelar | fff06c3 | 2014-11-06 06:55:14 -0800 | [diff] [blame] | 934 | err = execute_set_action(skb, key, nla_data(a)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 935 | break; |
| 936 | |
Jarno Rajahalme | 83d2b9b | 2015-02-05 13:40:49 -0800 | [diff] [blame] | 937 | case OVS_ACTION_ATTR_SET_MASKED: |
| 938 | case OVS_ACTION_ATTR_SET_TO_MASKED: |
| 939 | err = execute_masked_set_action(skb, key, nla_data(a)); |
| 940 | break; |
| 941 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 942 | case OVS_ACTION_ATTR_SAMPLE: |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 943 | err = sample(dp, skb, key, a, attr, len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 944 | break; |
| 945 | } |
| 946 | |
| 947 | if (unlikely(err)) { |
| 948 | kfree_skb(skb); |
| 949 | return err; |
| 950 | } |
| 951 | } |
| 952 | |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 953 | if (prev_port != -1) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 954 | do_output(dp, skb, prev_port); |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 955 | else |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 956 | consume_skb(skb); |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 961 | static void process_deferred_actions(struct datapath *dp) |
| 962 | { |
| 963 | struct action_fifo *fifo = this_cpu_ptr(action_fifos); |
| 964 | |
| 965 | /* Do not touch the FIFO in case there is no deferred actions. */ |
| 966 | if (action_fifo_is_empty(fifo)) |
| 967 | return; |
| 968 | |
| 969 | /* Finishing executing all deferred actions. */ |
| 970 | do { |
| 971 | struct deferred_action *da = action_fifo_get(fifo); |
| 972 | struct sk_buff *skb = da->skb; |
| 973 | struct sw_flow_key *key = &da->pkt_key; |
| 974 | const struct nlattr *actions = da->actions; |
| 975 | |
| 976 | if (actions) |
| 977 | do_execute_actions(dp, skb, key, actions, |
| 978 | nla_len(actions)); |
| 979 | else |
| 980 | ovs_dp_process_packet(skb, key); |
| 981 | } while (!action_fifo_is_empty(fifo)); |
| 982 | |
| 983 | /* Reset FIFO for the next packet. */ |
| 984 | action_fifo_init(fifo); |
| 985 | } |
| 986 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 987 | /* Execute a list of actions against 'skb'. */ |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame] | 988 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 989 | const struct sw_flow_actions *acts, |
| 990 | struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 991 | { |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 992 | int level = this_cpu_read(exec_actions_level); |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 993 | int err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 994 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 995 | this_cpu_inc(exec_actions_level); |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 996 | OVS_CB(skb)->egress_tun_info = NULL; |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 997 | err = do_execute_actions(dp, skb, key, |
| 998 | acts->actions, acts->actions_len); |
| 999 | |
| 1000 | if (!level) |
| 1001 | process_deferred_actions(dp); |
| 1002 | |
| 1003 | this_cpu_dec(exec_actions_level); |
| 1004 | return err; |
| 1005 | } |
| 1006 | |
| 1007 | int action_fifos_init(void) |
| 1008 | { |
| 1009 | action_fifos = alloc_percpu(struct action_fifo); |
| 1010 | if (!action_fifos) |
| 1011 | return -ENOMEM; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | void action_fifos_exit(void) |
| 1017 | { |
| 1018 | free_percpu(action_fifos); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1019 | } |