Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1 | /* |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 2 | * Copyright (c) 2007-2013 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> |
| 31 | #include <net/ip.h> |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 32 | #include <net/ipv6.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 33 | #include <net/checksum.h> |
| 34 | #include <net/dsfield.h> |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 35 | #include <net/sctp/checksum.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 36 | |
| 37 | #include "datapath.h" |
| 38 | #include "vport.h" |
| 39 | |
| 40 | 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^] | 41 | struct sw_flow_key *key, |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 42 | const struct nlattr *attr, int len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 43 | |
| 44 | static int make_writable(struct sk_buff *skb, int write_len) |
| 45 | { |
Jiri Benc | 2ba5af4 | 2014-08-21 21:33:44 +0200 | [diff] [blame] | 46 | if (!pskb_may_pull(skb, write_len)) |
| 47 | return -ENOMEM; |
| 48 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 49 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 50 | return 0; |
| 51 | |
| 52 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 53 | } |
| 54 | |
Joe Stringer | 39855b5 | 2012-08-31 15:28:28 -0700 | [diff] [blame] | 55 | /* remove VLAN header from packet and update csum accordingly. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 56 | static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci) |
| 57 | { |
| 58 | struct vlan_hdr *vhdr; |
| 59 | int err; |
| 60 | |
| 61 | err = make_writable(skb, VLAN_ETH_HLEN); |
| 62 | if (unlikely(err)) |
| 63 | return err; |
| 64 | |
| 65 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 66 | skb->csum = csum_sub(skb->csum, csum_partial(skb->data |
Cong Wang | 7b02408 | 2013-02-22 17:32:26 +0800 | [diff] [blame] | 67 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 68 | |
| 69 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
| 70 | *current_tci = vhdr->h_vlan_TCI; |
| 71 | |
| 72 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 73 | __skb_pull(skb, VLAN_HLEN); |
| 74 | |
| 75 | vlan_set_encap_proto(skb, vhdr); |
| 76 | skb->mac_header += VLAN_HLEN; |
Jiri Benc | 2ba5af4 | 2014-08-21 21:33:44 +0200 | [diff] [blame] | 77 | if (skb_network_offset(skb) < ETH_HLEN) |
| 78 | skb_set_network_header(skb, ETH_HLEN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 79 | skb_reset_mac_len(skb); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int pop_vlan(struct sk_buff *skb) |
| 85 | { |
| 86 | __be16 tci; |
| 87 | int err; |
| 88 | |
| 89 | if (likely(vlan_tx_tag_present(skb))) { |
| 90 | skb->vlan_tci = 0; |
| 91 | } else { |
| 92 | if (unlikely(skb->protocol != htons(ETH_P_8021Q) || |
| 93 | skb->len < VLAN_ETH_HLEN)) |
| 94 | return 0; |
| 95 | |
| 96 | err = __pop_vlan_tci(skb, &tci); |
| 97 | if (err) |
| 98 | return err; |
| 99 | } |
| 100 | /* move next vlan tag to hw accel tag */ |
| 101 | if (likely(skb->protocol != htons(ETH_P_8021Q) || |
| 102 | skb->len < VLAN_ETH_HLEN)) |
| 103 | return 0; |
| 104 | |
| 105 | err = __pop_vlan_tci(skb, &tci); |
| 106 | if (unlikely(err)) |
| 107 | return err; |
| 108 | |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 109 | __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan) |
| 114 | { |
| 115 | if (unlikely(vlan_tx_tag_present(skb))) { |
| 116 | u16 current_tag; |
| 117 | |
| 118 | /* push down current VLAN tag */ |
| 119 | current_tag = vlan_tx_tag_get(skb); |
| 120 | |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 121 | if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag)) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 122 | return -ENOMEM; |
| 123 | |
| 124 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 125 | skb->csum = csum_add(skb->csum, csum_partial(skb->data |
Cong Wang | 7b02408 | 2013-02-22 17:32:26 +0800 | [diff] [blame] | 126 | + (2 * ETH_ALEN), VLAN_HLEN, 0)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 127 | |
| 128 | } |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 129 | __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] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int set_eth_addr(struct sk_buff *skb, |
| 134 | const struct ovs_key_ethernet *eth_key) |
| 135 | { |
| 136 | int err; |
| 137 | err = make_writable(skb, ETH_HLEN); |
| 138 | if (unlikely(err)) |
| 139 | return err; |
| 140 | |
Pravin B Shelar | b34df5e | 2013-06-13 11:11:44 -0700 | [diff] [blame] | 141 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
| 142 | |
Joe Perches | 8c63ff0 | 2014-02-18 11:15:45 -0800 | [diff] [blame] | 143 | ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src); |
| 144 | ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 145 | |
Pravin B Shelar | b34df5e | 2013-06-13 11:11:44 -0700 | [diff] [blame] | 146 | ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2); |
| 147 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
| 152 | __be32 *addr, __be32 new_addr) |
| 153 | { |
| 154 | int transport_len = skb->len - skb_transport_offset(skb); |
| 155 | |
| 156 | if (nh->protocol == IPPROTO_TCP) { |
| 157 | if (likely(transport_len >= sizeof(struct tcphdr))) |
| 158 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
| 159 | *addr, new_addr, 1); |
| 160 | } else if (nh->protocol == IPPROTO_UDP) { |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 161 | if (likely(transport_len >= sizeof(struct udphdr))) { |
| 162 | struct udphdr *uh = udp_hdr(skb); |
| 163 | |
| 164 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
| 165 | inet_proto_csum_replace4(&uh->check, skb, |
| 166 | *addr, new_addr, 1); |
| 167 | if (!uh->check) |
| 168 | uh->check = CSUM_MANGLED_0; |
| 169 | } |
| 170 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | csum_replace4(&nh->check, *addr, new_addr); |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 174 | skb_clear_hash(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 175 | *addr = new_addr; |
| 176 | } |
| 177 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 178 | static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto, |
| 179 | __be32 addr[4], const __be32 new_addr[4]) |
| 180 | { |
| 181 | int transport_len = skb->len - skb_transport_offset(skb); |
| 182 | |
| 183 | if (l4_proto == IPPROTO_TCP) { |
| 184 | if (likely(transport_len >= sizeof(struct tcphdr))) |
| 185 | inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb, |
| 186 | addr, new_addr, 1); |
| 187 | } else if (l4_proto == IPPROTO_UDP) { |
| 188 | if (likely(transport_len >= sizeof(struct udphdr))) { |
| 189 | struct udphdr *uh = udp_hdr(skb); |
| 190 | |
| 191 | if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) { |
| 192 | inet_proto_csum_replace16(&uh->check, skb, |
| 193 | addr, new_addr, 1); |
| 194 | if (!uh->check) |
| 195 | uh->check = CSUM_MANGLED_0; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto, |
| 202 | __be32 addr[4], const __be32 new_addr[4], |
| 203 | bool recalculate_csum) |
| 204 | { |
| 205 | if (recalculate_csum) |
| 206 | update_ipv6_checksum(skb, l4_proto, addr, new_addr); |
| 207 | |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 208 | skb_clear_hash(skb); |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 209 | memcpy(addr, new_addr, sizeof(__be32[4])); |
| 210 | } |
| 211 | |
| 212 | static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc) |
| 213 | { |
| 214 | nh->priority = tc >> 4; |
| 215 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4); |
| 216 | } |
| 217 | |
| 218 | static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl) |
| 219 | { |
| 220 | nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16; |
| 221 | nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8; |
| 222 | nh->flow_lbl[2] = fl & 0x000000FF; |
| 223 | } |
| 224 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 225 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl) |
| 226 | { |
| 227 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); |
| 228 | nh->ttl = new_ttl; |
| 229 | } |
| 230 | |
| 231 | static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key) |
| 232 | { |
| 233 | struct iphdr *nh; |
| 234 | int err; |
| 235 | |
| 236 | err = make_writable(skb, skb_network_offset(skb) + |
| 237 | sizeof(struct iphdr)); |
| 238 | if (unlikely(err)) |
| 239 | return err; |
| 240 | |
| 241 | nh = ip_hdr(skb); |
| 242 | |
| 243 | if (ipv4_key->ipv4_src != nh->saddr) |
| 244 | set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src); |
| 245 | |
| 246 | if (ipv4_key->ipv4_dst != nh->daddr) |
| 247 | set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst); |
| 248 | |
| 249 | if (ipv4_key->ipv4_tos != nh->tos) |
| 250 | ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos); |
| 251 | |
| 252 | if (ipv4_key->ipv4_ttl != nh->ttl) |
| 253 | set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 258 | static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key) |
| 259 | { |
| 260 | struct ipv6hdr *nh; |
| 261 | int err; |
| 262 | __be32 *saddr; |
| 263 | __be32 *daddr; |
| 264 | |
| 265 | err = make_writable(skb, skb_network_offset(skb) + |
| 266 | sizeof(struct ipv6hdr)); |
| 267 | if (unlikely(err)) |
| 268 | return err; |
| 269 | |
| 270 | nh = ipv6_hdr(skb); |
| 271 | saddr = (__be32 *)&nh->saddr; |
| 272 | daddr = (__be32 *)&nh->daddr; |
| 273 | |
| 274 | if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) |
| 275 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr, |
| 276 | ipv6_key->ipv6_src, true); |
| 277 | |
| 278 | if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) { |
| 279 | unsigned int offset = 0; |
| 280 | int flags = IP6_FH_F_SKIP_RH; |
| 281 | bool recalc_csum = true; |
| 282 | |
| 283 | if (ipv6_ext_hdr(nh->nexthdr)) |
| 284 | recalc_csum = ipv6_find_hdr(skb, &offset, |
| 285 | NEXTHDR_ROUTING, NULL, |
| 286 | &flags) != NEXTHDR_ROUTING; |
| 287 | |
| 288 | set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr, |
| 289 | ipv6_key->ipv6_dst, recalc_csum); |
| 290 | } |
| 291 | |
| 292 | set_ipv6_tc(nh, ipv6_key->ipv6_tclass); |
| 293 | set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label)); |
| 294 | nh->hop_limit = ipv6_key->ipv6_hlimit; |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 299 | /* Must follow make_writable() since that can move the skb data. */ |
| 300 | static void set_tp_port(struct sk_buff *skb, __be16 *port, |
| 301 | __be16 new_port, __sum16 *check) |
| 302 | { |
| 303 | inet_proto_csum_replace2(check, skb, *port, new_port, 0); |
| 304 | *port = new_port; |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 305 | skb_clear_hash(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 308 | static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port) |
| 309 | { |
| 310 | struct udphdr *uh = udp_hdr(skb); |
| 311 | |
| 312 | if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) { |
| 313 | set_tp_port(skb, port, new_port, &uh->check); |
| 314 | |
| 315 | if (!uh->check) |
| 316 | uh->check = CSUM_MANGLED_0; |
| 317 | } else { |
| 318 | *port = new_port; |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 319 | skb_clear_hash(skb); |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 324 | { |
| 325 | struct udphdr *uh; |
| 326 | int err; |
| 327 | |
| 328 | err = make_writable(skb, skb_transport_offset(skb) + |
| 329 | sizeof(struct udphdr)); |
| 330 | if (unlikely(err)) |
| 331 | return err; |
| 332 | |
| 333 | uh = udp_hdr(skb); |
| 334 | if (udp_port_key->udp_src != uh->source) |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 335 | set_udp_port(skb, &uh->source, udp_port_key->udp_src); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 336 | |
| 337 | if (udp_port_key->udp_dst != uh->dest) |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 338 | set_udp_port(skb, &uh->dest, udp_port_key->udp_dst); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 343 | static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 344 | { |
| 345 | struct tcphdr *th; |
| 346 | int err; |
| 347 | |
| 348 | err = make_writable(skb, skb_transport_offset(skb) + |
| 349 | sizeof(struct tcphdr)); |
| 350 | if (unlikely(err)) |
| 351 | return err; |
| 352 | |
| 353 | th = tcp_hdr(skb); |
| 354 | if (tcp_port_key->tcp_src != th->source) |
| 355 | set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check); |
| 356 | |
| 357 | if (tcp_port_key->tcp_dst != th->dest) |
| 358 | set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 363 | static int set_sctp(struct sk_buff *skb, |
| 364 | const struct ovs_key_sctp *sctp_port_key) |
| 365 | { |
| 366 | struct sctphdr *sh; |
| 367 | int err; |
| 368 | unsigned int sctphoff = skb_transport_offset(skb); |
| 369 | |
| 370 | err = make_writable(skb, sctphoff + sizeof(struct sctphdr)); |
| 371 | if (unlikely(err)) |
| 372 | return err; |
| 373 | |
| 374 | sh = sctp_hdr(skb); |
| 375 | if (sctp_port_key->sctp_src != sh->source || |
| 376 | sctp_port_key->sctp_dst != sh->dest) { |
| 377 | __le32 old_correct_csum, new_csum, old_csum; |
| 378 | |
| 379 | old_csum = sh->checksum; |
| 380 | old_correct_csum = sctp_compute_cksum(skb, sctphoff); |
| 381 | |
| 382 | sh->source = sctp_port_key->sctp_src; |
| 383 | sh->dest = sctp_port_key->sctp_dst; |
| 384 | |
| 385 | new_csum = sctp_compute_cksum(skb, sctphoff); |
| 386 | |
| 387 | /* Carry any checksum errors through. */ |
| 388 | sh->checksum = old_csum ^ old_correct_csum ^ new_csum; |
| 389 | |
Tom Herbert | 7539fad | 2013-12-15 22:12:18 -0800 | [diff] [blame] | 390 | skb_clear_hash(skb); |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 396 | static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
| 397 | { |
| 398 | struct vport *vport; |
| 399 | |
| 400 | if (unlikely(!skb)) |
| 401 | return -ENOMEM; |
| 402 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 403 | vport = ovs_vport_rcu(dp, out_port); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 404 | if (unlikely(!vport)) { |
| 405 | kfree_skb(skb); |
| 406 | return -ENODEV; |
| 407 | } |
| 408 | |
| 409 | ovs_vport_send(vport, skb); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 414 | struct sw_flow_key *key, const struct nlattr *attr) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 415 | { |
| 416 | struct dp_upcall_info upcall; |
| 417 | const struct nlattr *a; |
| 418 | int rem; |
| 419 | |
| 420 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 421 | upcall.key = key; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 422 | upcall.userdata = NULL; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 423 | upcall.portid = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 424 | |
| 425 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 426 | a = nla_next(a, &rem)) { |
| 427 | switch (nla_type(a)) { |
| 428 | case OVS_USERSPACE_ATTR_USERDATA: |
| 429 | upcall.userdata = a; |
| 430 | break; |
| 431 | |
| 432 | case OVS_USERSPACE_ATTR_PID: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 433 | upcall.portid = nla_get_u32(a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return ovs_dp_upcall(dp, skb, &upcall); |
| 439 | } |
| 440 | |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 441 | static bool last_action(const struct nlattr *a, int rem) |
| 442 | { |
| 443 | return a->nla_len == rem; |
| 444 | } |
| 445 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 446 | static int sample(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 447 | struct sw_flow_key *key, const struct nlattr *attr) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 448 | { |
| 449 | const struct nlattr *acts_list = NULL; |
| 450 | const struct nlattr *a; |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 451 | struct sk_buff *sample_skb; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 452 | int rem; |
| 453 | |
| 454 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 455 | a = nla_next(a, &rem)) { |
| 456 | switch (nla_type(a)) { |
| 457 | case OVS_SAMPLE_ATTR_PROBABILITY: |
Aruna-Hewapathirane | 63862b5 | 2014-01-11 07:15:59 -0500 | [diff] [blame] | 458 | if (prandom_u32() >= nla_get_u32(a)) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | break; |
| 461 | |
| 462 | case OVS_SAMPLE_ATTR_ACTIONS: |
| 463 | acts_list = a; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 468 | rem = nla_len(acts_list); |
| 469 | a = nla_data(acts_list); |
| 470 | |
| 471 | /* Actions list is either empty or only contains a single user-space |
| 472 | * action, the latter being a special case as it is the only known |
| 473 | * usage of the sample action. |
| 474 | * In these special cases don't clone the skb as there are no |
| 475 | * side-effects in the nested actions. |
| 476 | * Otherwise, clone in case the nested actions have side effects. |
| 477 | */ |
| 478 | if (likely(rem == 0 || (nla_type(a) == OVS_ACTION_ATTR_USERSPACE && |
| 479 | last_action(a, rem)))) { |
| 480 | sample_skb = skb; |
| 481 | skb_get(skb); |
| 482 | } else { |
| 483 | sample_skb = skb_clone(skb, GFP_ATOMIC); |
Andy Zhou | d9e0ecb | 2014-07-17 15:17:54 -0700 | [diff] [blame] | 484 | if (!sample_skb) /* Skip sample action when out of memory. */ |
| 485 | return 0; |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* Note that do_execute_actions() never consumes skb. |
| 489 | * In the case where skb has been cloned above it is the clone that |
| 490 | * is consumed. Otherwise the skb_get(skb) call prevents |
| 491 | * consumption by do_execute_actions(). Thus, it is safe to simply |
| 492 | * return the error code and let the caller (also |
| 493 | * do_execute_actions()) free skb on error. |
| 494 | */ |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 495 | return do_execute_actions(dp, sample_skb, key, a, rem); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static int execute_set_action(struct sk_buff *skb, |
| 499 | const struct nlattr *nested_attr) |
| 500 | { |
| 501 | int err = 0; |
| 502 | |
| 503 | switch (nla_type(nested_attr)) { |
| 504 | case OVS_KEY_ATTR_PRIORITY: |
| 505 | skb->priority = nla_get_u32(nested_attr); |
| 506 | break; |
| 507 | |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 508 | case OVS_KEY_ATTR_SKB_MARK: |
| 509 | skb->mark = nla_get_u32(nested_attr); |
| 510 | break; |
| 511 | |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 512 | case OVS_KEY_ATTR_IPV4_TUNNEL: |
| 513 | OVS_CB(skb)->tun_key = nla_data(nested_attr); |
| 514 | break; |
| 515 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 516 | case OVS_KEY_ATTR_ETHERNET: |
| 517 | err = set_eth_addr(skb, nla_data(nested_attr)); |
| 518 | break; |
| 519 | |
| 520 | case OVS_KEY_ATTR_IPV4: |
| 521 | err = set_ipv4(skb, nla_data(nested_attr)); |
| 522 | break; |
| 523 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 524 | case OVS_KEY_ATTR_IPV6: |
| 525 | err = set_ipv6(skb, nla_data(nested_attr)); |
| 526 | break; |
| 527 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 528 | case OVS_KEY_ATTR_TCP: |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 529 | err = set_tcp(skb, nla_data(nested_attr)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 530 | break; |
| 531 | |
| 532 | case OVS_KEY_ATTR_UDP: |
Jesse Gross | 81e5d41 | 2012-03-06 15:05:46 -0800 | [diff] [blame] | 533 | err = set_udp(skb, nla_data(nested_attr)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 534 | break; |
Joe Stringer | a175a72 | 2013-08-22 12:30:48 -0700 | [diff] [blame] | 535 | |
| 536 | case OVS_KEY_ATTR_SCTP: |
| 537 | err = set_sctp(skb, nla_data(nested_attr)); |
| 538 | break; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | /* Execute a list of actions against 'skb'. */ |
| 545 | 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^] | 546 | struct sw_flow_key *key, |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 547 | const struct nlattr *attr, int len) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 548 | { |
| 549 | /* Every output action needs a separate clone of 'skb', but the common |
| 550 | * case is just a single output action, so that doing a clone and |
| 551 | * then freeing the original skbuff is wasteful. So the following code |
| 552 | * is slightly obscure just to avoid that. */ |
| 553 | int prev_port = -1; |
| 554 | const struct nlattr *a; |
| 555 | int rem; |
| 556 | |
| 557 | for (a = attr, rem = len; rem > 0; |
| 558 | a = nla_next(a, &rem)) { |
| 559 | int err = 0; |
| 560 | |
| 561 | if (prev_port != -1) { |
| 562 | do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port); |
| 563 | prev_port = -1; |
| 564 | } |
| 565 | |
| 566 | switch (nla_type(a)) { |
| 567 | case OVS_ACTION_ATTR_OUTPUT: |
| 568 | prev_port = nla_get_u32(a); |
| 569 | break; |
| 570 | |
| 571 | case OVS_ACTION_ATTR_USERSPACE: |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 572 | output_userspace(dp, skb, key, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 573 | break; |
| 574 | |
| 575 | case OVS_ACTION_ATTR_PUSH_VLAN: |
| 576 | err = push_vlan(skb, nla_data(a)); |
| 577 | if (unlikely(err)) /* skb already freed. */ |
| 578 | return err; |
| 579 | break; |
| 580 | |
| 581 | case OVS_ACTION_ATTR_POP_VLAN: |
| 582 | err = pop_vlan(skb); |
| 583 | break; |
| 584 | |
| 585 | case OVS_ACTION_ATTR_SET: |
| 586 | err = execute_set_action(skb, nla_data(a)); |
| 587 | break; |
| 588 | |
| 589 | case OVS_ACTION_ATTR_SAMPLE: |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 590 | err = sample(dp, skb, key, a); |
Andy Zhou | fe984c0 | 2014-05-06 17:23:48 -0700 | [diff] [blame] | 591 | if (unlikely(err)) /* skb already freed. */ |
| 592 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 593 | break; |
| 594 | } |
| 595 | |
| 596 | if (unlikely(err)) { |
| 597 | kfree_skb(skb); |
| 598 | return err; |
| 599 | } |
| 600 | } |
| 601 | |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 602 | if (prev_port != -1) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 603 | do_output(dp, skb, prev_port); |
Simon Horman | 651887b | 2014-07-21 15:12:34 -0700 | [diff] [blame] | 604 | else |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 605 | consume_skb(skb); |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | /* Execute a list of actions against 'skb'. */ |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 611 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, |
| 612 | struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 613 | { |
| 614 | struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts); |
| 615 | |
Jesse Gross | 36bf5cc | 2013-08-14 15:50:36 -0700 | [diff] [blame] | 616 | OVS_CB(skb)->tun_key = NULL; |
Pravin B Shelar | 2ff3e4e | 2014-09-15 19:15:28 -0700 | [diff] [blame^] | 617 | return do_execute_actions(dp, skb, key, |
| 618 | acts->actions, acts->actions_len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 619 | } |