actions.c 9.6 KB

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