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