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