Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007-2011 Nicira Networks. |
| 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> |
| 25 | #include <linux/tcp.h> |
| 26 | #include <linux/udp.h> |
| 27 | #include <linux/in6.h> |
| 28 | #include <linux/if_arp.h> |
| 29 | #include <linux/if_vlan.h> |
| 30 | #include <net/ip.h> |
| 31 | #include <net/checksum.h> |
| 32 | #include <net/dsfield.h> |
| 33 | |
| 34 | #include "datapath.h" |
| 35 | #include "vport.h" |
| 36 | |
| 37 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
| 38 | const struct nlattr *attr, int len, bool keep_skb); |
| 39 | |
| 40 | static int make_writable(struct sk_buff *skb, int write_len) |
| 41 | { |
| 42 | if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) |
| 43 | return 0; |
| 44 | |
| 45 | return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 46 | } |
| 47 | |
| 48 | /* remove VLAN header from packet and update csum accrodingly. */ |
| 49 | static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci) |
| 50 | { |
| 51 | struct vlan_hdr *vhdr; |
| 52 | int err; |
| 53 | |
| 54 | err = make_writable(skb, VLAN_ETH_HLEN); |
| 55 | if (unlikely(err)) |
| 56 | return err; |
| 57 | |
| 58 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 59 | skb->csum = csum_sub(skb->csum, csum_partial(skb->data |
| 60 | + ETH_HLEN, VLAN_HLEN, 0)); |
| 61 | |
| 62 | vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); |
| 63 | *current_tci = vhdr->h_vlan_TCI; |
| 64 | |
| 65 | memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); |
| 66 | __skb_pull(skb, VLAN_HLEN); |
| 67 | |
| 68 | vlan_set_encap_proto(skb, vhdr); |
| 69 | skb->mac_header += VLAN_HLEN; |
| 70 | skb_reset_mac_len(skb); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int pop_vlan(struct sk_buff *skb) |
| 76 | { |
| 77 | __be16 tci; |
| 78 | int err; |
| 79 | |
| 80 | if (likely(vlan_tx_tag_present(skb))) { |
| 81 | skb->vlan_tci = 0; |
| 82 | } else { |
| 83 | if (unlikely(skb->protocol != htons(ETH_P_8021Q) || |
| 84 | skb->len < VLAN_ETH_HLEN)) |
| 85 | return 0; |
| 86 | |
| 87 | err = __pop_vlan_tci(skb, &tci); |
| 88 | if (err) |
| 89 | return err; |
| 90 | } |
| 91 | /* move next vlan tag to hw accel tag */ |
| 92 | if (likely(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 (unlikely(err)) |
| 98 | return err; |
| 99 | |
| 100 | __vlan_hwaccel_put_tag(skb, ntohs(tci)); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan) |
| 105 | { |
| 106 | if (unlikely(vlan_tx_tag_present(skb))) { |
| 107 | u16 current_tag; |
| 108 | |
| 109 | /* push down current VLAN tag */ |
| 110 | current_tag = vlan_tx_tag_get(skb); |
| 111 | |
| 112 | if (!__vlan_put_tag(skb, current_tag)) |
| 113 | return -ENOMEM; |
| 114 | |
| 115 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
| 116 | skb->csum = csum_add(skb->csum, csum_partial(skb->data |
| 117 | + ETH_HLEN, VLAN_HLEN, 0)); |
| 118 | |
| 119 | } |
| 120 | __vlan_hwaccel_put_tag(skb, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int set_eth_addr(struct sk_buff *skb, |
| 125 | const struct ovs_key_ethernet *eth_key) |
| 126 | { |
| 127 | int err; |
| 128 | err = make_writable(skb, ETH_HLEN); |
| 129 | if (unlikely(err)) |
| 130 | return err; |
| 131 | |
| 132 | memcpy(eth_hdr(skb)->h_source, eth_key->eth_src, ETH_ALEN); |
| 133 | memcpy(eth_hdr(skb)->h_dest, eth_key->eth_dst, ETH_ALEN); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh, |
| 139 | __be32 *addr, __be32 new_addr) |
| 140 | { |
| 141 | int transport_len = skb->len - skb_transport_offset(skb); |
| 142 | |
| 143 | if (nh->protocol == IPPROTO_TCP) { |
| 144 | if (likely(transport_len >= sizeof(struct tcphdr))) |
| 145 | inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb, |
| 146 | *addr, new_addr, 1); |
| 147 | } else if (nh->protocol == IPPROTO_UDP) { |
| 148 | if (likely(transport_len >= sizeof(struct udphdr))) |
| 149 | inet_proto_csum_replace4(&udp_hdr(skb)->check, skb, |
| 150 | *addr, new_addr, 1); |
| 151 | } |
| 152 | |
| 153 | csum_replace4(&nh->check, *addr, new_addr); |
| 154 | skb->rxhash = 0; |
| 155 | *addr = new_addr; |
| 156 | } |
| 157 | |
| 158 | static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl) |
| 159 | { |
| 160 | csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8)); |
| 161 | nh->ttl = new_ttl; |
| 162 | } |
| 163 | |
| 164 | static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key) |
| 165 | { |
| 166 | struct iphdr *nh; |
| 167 | int err; |
| 168 | |
| 169 | err = make_writable(skb, skb_network_offset(skb) + |
| 170 | sizeof(struct iphdr)); |
| 171 | if (unlikely(err)) |
| 172 | return err; |
| 173 | |
| 174 | nh = ip_hdr(skb); |
| 175 | |
| 176 | if (ipv4_key->ipv4_src != nh->saddr) |
| 177 | set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src); |
| 178 | |
| 179 | if (ipv4_key->ipv4_dst != nh->daddr) |
| 180 | set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst); |
| 181 | |
| 182 | if (ipv4_key->ipv4_tos != nh->tos) |
| 183 | ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos); |
| 184 | |
| 185 | if (ipv4_key->ipv4_ttl != nh->ttl) |
| 186 | set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* Must follow make_writable() since that can move the skb data. */ |
| 192 | static void set_tp_port(struct sk_buff *skb, __be16 *port, |
| 193 | __be16 new_port, __sum16 *check) |
| 194 | { |
| 195 | inet_proto_csum_replace2(check, skb, *port, new_port, 0); |
| 196 | *port = new_port; |
| 197 | skb->rxhash = 0; |
| 198 | } |
| 199 | |
| 200 | static int set_udp_port(struct sk_buff *skb, |
| 201 | const struct ovs_key_udp *udp_port_key) |
| 202 | { |
| 203 | struct udphdr *uh; |
| 204 | int err; |
| 205 | |
| 206 | err = make_writable(skb, skb_transport_offset(skb) + |
| 207 | sizeof(struct udphdr)); |
| 208 | if (unlikely(err)) |
| 209 | return err; |
| 210 | |
| 211 | uh = udp_hdr(skb); |
| 212 | if (udp_port_key->udp_src != uh->source) |
| 213 | set_tp_port(skb, &uh->source, udp_port_key->udp_src, &uh->check); |
| 214 | |
| 215 | if (udp_port_key->udp_dst != uh->dest) |
| 216 | set_tp_port(skb, &uh->dest, udp_port_key->udp_dst, &uh->check); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int set_tcp_port(struct sk_buff *skb, |
| 222 | const struct ovs_key_tcp *tcp_port_key) |
| 223 | { |
| 224 | struct tcphdr *th; |
| 225 | int err; |
| 226 | |
| 227 | err = make_writable(skb, skb_transport_offset(skb) + |
| 228 | sizeof(struct tcphdr)); |
| 229 | if (unlikely(err)) |
| 230 | return err; |
| 231 | |
| 232 | th = tcp_hdr(skb); |
| 233 | if (tcp_port_key->tcp_src != th->source) |
| 234 | set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check); |
| 235 | |
| 236 | if (tcp_port_key->tcp_dst != th->dest) |
| 237 | set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port) |
| 243 | { |
| 244 | struct vport *vport; |
| 245 | |
| 246 | if (unlikely(!skb)) |
| 247 | return -ENOMEM; |
| 248 | |
| 249 | vport = rcu_dereference(dp->ports[out_port]); |
| 250 | if (unlikely(!vport)) { |
| 251 | kfree_skb(skb); |
| 252 | return -ENODEV; |
| 253 | } |
| 254 | |
| 255 | ovs_vport_send(vport, skb); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
| 260 | const struct nlattr *attr) |
| 261 | { |
| 262 | struct dp_upcall_info upcall; |
| 263 | const struct nlattr *a; |
| 264 | int rem; |
| 265 | |
| 266 | upcall.cmd = OVS_PACKET_CMD_ACTION; |
| 267 | upcall.key = &OVS_CB(skb)->flow->key; |
| 268 | upcall.userdata = NULL; |
| 269 | upcall.pid = 0; |
| 270 | |
| 271 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 272 | a = nla_next(a, &rem)) { |
| 273 | switch (nla_type(a)) { |
| 274 | case OVS_USERSPACE_ATTR_USERDATA: |
| 275 | upcall.userdata = a; |
| 276 | break; |
| 277 | |
| 278 | case OVS_USERSPACE_ATTR_PID: |
| 279 | upcall.pid = nla_get_u32(a); |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return ovs_dp_upcall(dp, skb, &upcall); |
| 285 | } |
| 286 | |
| 287 | static int sample(struct datapath *dp, struct sk_buff *skb, |
| 288 | const struct nlattr *attr) |
| 289 | { |
| 290 | const struct nlattr *acts_list = NULL; |
| 291 | const struct nlattr *a; |
| 292 | int rem; |
| 293 | |
| 294 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 295 | a = nla_next(a, &rem)) { |
| 296 | switch (nla_type(a)) { |
| 297 | case OVS_SAMPLE_ATTR_PROBABILITY: |
| 298 | if (net_random() >= nla_get_u32(a)) |
| 299 | return 0; |
| 300 | break; |
| 301 | |
| 302 | case OVS_SAMPLE_ATTR_ACTIONS: |
| 303 | acts_list = a; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return do_execute_actions(dp, skb, nla_data(acts_list), |
| 309 | nla_len(acts_list), true); |
| 310 | } |
| 311 | |
| 312 | static int execute_set_action(struct sk_buff *skb, |
| 313 | const struct nlattr *nested_attr) |
| 314 | { |
| 315 | int err = 0; |
| 316 | |
| 317 | switch (nla_type(nested_attr)) { |
| 318 | case OVS_KEY_ATTR_PRIORITY: |
| 319 | skb->priority = nla_get_u32(nested_attr); |
| 320 | break; |
| 321 | |
| 322 | case OVS_KEY_ATTR_ETHERNET: |
| 323 | err = set_eth_addr(skb, nla_data(nested_attr)); |
| 324 | break; |
| 325 | |
| 326 | case OVS_KEY_ATTR_IPV4: |
| 327 | err = set_ipv4(skb, nla_data(nested_attr)); |
| 328 | break; |
| 329 | |
| 330 | case OVS_KEY_ATTR_TCP: |
| 331 | err = set_tcp_port(skb, nla_data(nested_attr)); |
| 332 | break; |
| 333 | |
| 334 | case OVS_KEY_ATTR_UDP: |
| 335 | err = set_udp_port(skb, nla_data(nested_attr)); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return err; |
| 340 | } |
| 341 | |
| 342 | /* Execute a list of actions against 'skb'. */ |
| 343 | static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, |
| 344 | const struct nlattr *attr, int len, bool keep_skb) |
| 345 | { |
| 346 | /* Every output action needs a separate clone of 'skb', but the common |
| 347 | * case is just a single output action, so that doing a clone and |
| 348 | * then freeing the original skbuff is wasteful. So the following code |
| 349 | * is slightly obscure just to avoid that. */ |
| 350 | int prev_port = -1; |
| 351 | const struct nlattr *a; |
| 352 | int rem; |
| 353 | |
| 354 | for (a = attr, rem = len; rem > 0; |
| 355 | a = nla_next(a, &rem)) { |
| 356 | int err = 0; |
| 357 | |
| 358 | if (prev_port != -1) { |
| 359 | do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port); |
| 360 | prev_port = -1; |
| 361 | } |
| 362 | |
| 363 | switch (nla_type(a)) { |
| 364 | case OVS_ACTION_ATTR_OUTPUT: |
| 365 | prev_port = nla_get_u32(a); |
| 366 | break; |
| 367 | |
| 368 | case OVS_ACTION_ATTR_USERSPACE: |
| 369 | output_userspace(dp, skb, a); |
| 370 | break; |
| 371 | |
| 372 | case OVS_ACTION_ATTR_PUSH_VLAN: |
| 373 | err = push_vlan(skb, nla_data(a)); |
| 374 | if (unlikely(err)) /* skb already freed. */ |
| 375 | return err; |
| 376 | break; |
| 377 | |
| 378 | case OVS_ACTION_ATTR_POP_VLAN: |
| 379 | err = pop_vlan(skb); |
| 380 | break; |
| 381 | |
| 382 | case OVS_ACTION_ATTR_SET: |
| 383 | err = execute_set_action(skb, nla_data(a)); |
| 384 | break; |
| 385 | |
| 386 | case OVS_ACTION_ATTR_SAMPLE: |
| 387 | err = sample(dp, skb, a); |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | if (unlikely(err)) { |
| 392 | kfree_skb(skb); |
| 393 | return err; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (prev_port != -1) { |
| 398 | if (keep_skb) |
| 399 | skb = skb_clone(skb, GFP_ATOMIC); |
| 400 | |
| 401 | do_output(dp, skb, prev_port); |
| 402 | } else if (!keep_skb) |
| 403 | consume_skb(skb); |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /* Execute a list of actions against 'skb'. */ |
| 409 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb) |
| 410 | { |
| 411 | struct sw_flow_actions *acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts); |
| 412 | |
| 413 | return do_execute_actions(dp, skb, acts->actions, |
| 414 | acts->actions_len, false); |
| 415 | } |