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