actions.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  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/sctp.h>
  24. #include <linux/tcp.h>
  25. #include <linux/udp.h>
  26. #include <linux/in6.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/if_vlan.h>
  29. #include <net/ip.h>
  30. #include <net/ipv6.h>
  31. #include <net/checksum.h>
  32. #include <net/dsfield.h>
  33. #include <net/mpls.h>
  34. #include <net/sctp/checksum.h>
  35. #include "datapath.h"
  36. #include "flow.h"
  37. #include "vport.h"
  38. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  39. struct sw_flow_key *key,
  40. const struct nlattr *attr, int len);
  41. struct deferred_action {
  42. struct sk_buff *skb;
  43. const struct nlattr *actions;
  44. /* Store pkt_key clone when creating deferred action. */
  45. struct sw_flow_key pkt_key;
  46. };
  47. #define DEFERRED_ACTION_FIFO_SIZE 10
  48. struct action_fifo {
  49. int head;
  50. int tail;
  51. /* Deferred action fifo queue storage. */
  52. struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
  53. };
  54. static struct action_fifo __percpu *action_fifos;
  55. static DEFINE_PER_CPU(int, exec_actions_level);
  56. static void action_fifo_init(struct action_fifo *fifo)
  57. {
  58. fifo->head = 0;
  59. fifo->tail = 0;
  60. }
  61. static bool action_fifo_is_empty(const struct action_fifo *fifo)
  62. {
  63. return (fifo->head == fifo->tail);
  64. }
  65. static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
  66. {
  67. if (action_fifo_is_empty(fifo))
  68. return NULL;
  69. return &fifo->fifo[fifo->tail++];
  70. }
  71. static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
  72. {
  73. if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
  74. return NULL;
  75. return &fifo->fifo[fifo->head++];
  76. }
  77. /* Return true if fifo is not full */
  78. static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
  79. const struct sw_flow_key *key,
  80. const struct nlattr *attr)
  81. {
  82. struct action_fifo *fifo;
  83. struct deferred_action *da;
  84. fifo = this_cpu_ptr(action_fifos);
  85. da = action_fifo_put(fifo);
  86. if (da) {
  87. da->skb = skb;
  88. da->actions = attr;
  89. da->pkt_key = *key;
  90. }
  91. return da;
  92. }
  93. static void invalidate_flow_key(struct sw_flow_key *key)
  94. {
  95. key->eth.type = htons(0);
  96. }
  97. static bool is_flow_key_valid(const struct sw_flow_key *key)
  98. {
  99. return !!key->eth.type;
  100. }
  101. static int make_writable(struct sk_buff *skb, int write_len)
  102. {
  103. if (!pskb_may_pull(skb, write_len))
  104. return -ENOMEM;
  105. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  106. return 0;
  107. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  108. }
  109. static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  110. const struct ovs_action_push_mpls *mpls)
  111. {
  112. __be32 *new_mpls_lse;
  113. struct ethhdr *hdr;
  114. /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
  115. if (skb->encapsulation)
  116. return -ENOTSUPP;
  117. if (skb_cow_head(skb, MPLS_HLEN) < 0)
  118. return -ENOMEM;
  119. skb_push(skb, MPLS_HLEN);
  120. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  121. skb->mac_len);
  122. skb_reset_mac_header(skb);
  123. new_mpls_lse = (__be32 *)skb_mpls_header(skb);
  124. *new_mpls_lse = mpls->mpls_lse;
  125. if (skb->ip_summed == CHECKSUM_COMPLETE)
  126. skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
  127. MPLS_HLEN, 0));
  128. hdr = eth_hdr(skb);
  129. hdr->h_proto = mpls->mpls_ethertype;
  130. skb_set_inner_protocol(skb, skb->protocol);
  131. skb->protocol = mpls->mpls_ethertype;
  132. invalidate_flow_key(key);
  133. return 0;
  134. }
  135. static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  136. const __be16 ethertype)
  137. {
  138. struct ethhdr *hdr;
  139. int err;
  140. err = make_writable(skb, skb->mac_len + MPLS_HLEN);
  141. if (unlikely(err))
  142. return err;
  143. skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
  144. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  145. skb->mac_len);
  146. __skb_pull(skb, MPLS_HLEN);
  147. skb_reset_mac_header(skb);
  148. /* skb_mpls_header() is used to locate the ethertype
  149. * field correctly in the presence of VLAN tags.
  150. */
  151. hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
  152. hdr->h_proto = ethertype;
  153. if (eth_p_mpls(skb->protocol))
  154. skb->protocol = ethertype;
  155. invalidate_flow_key(key);
  156. return 0;
  157. }
  158. static int set_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  159. const __be32 *mpls_lse)
  160. {
  161. __be32 *stack;
  162. int err;
  163. err = make_writable(skb, skb->mac_len + MPLS_HLEN);
  164. if (unlikely(err))
  165. return err;
  166. stack = (__be32 *)skb_mpls_header(skb);
  167. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  168. __be32 diff[] = { ~(*stack), *mpls_lse };
  169. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  170. ~skb->csum);
  171. }
  172. *stack = *mpls_lse;
  173. key->mpls.top_lse = *mpls_lse;
  174. return 0;
  175. }
  176. /* remove VLAN header from packet and update csum accordingly. */
  177. static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
  178. {
  179. struct vlan_hdr *vhdr;
  180. int err;
  181. err = make_writable(skb, VLAN_ETH_HLEN);
  182. if (unlikely(err))
  183. return err;
  184. skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  185. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  186. *current_tci = vhdr->h_vlan_TCI;
  187. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  188. __skb_pull(skb, VLAN_HLEN);
  189. vlan_set_encap_proto(skb, vhdr);
  190. skb->mac_header += VLAN_HLEN;
  191. if (skb_network_offset(skb) < ETH_HLEN)
  192. skb_set_network_header(skb, ETH_HLEN);
  193. /* Update mac_len for subsequent MPLS actions */
  194. skb_reset_mac_len(skb);
  195. return 0;
  196. }
  197. static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  198. {
  199. __be16 tci;
  200. int err;
  201. if (likely(vlan_tx_tag_present(skb))) {
  202. skb->vlan_tci = 0;
  203. } else {
  204. if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
  205. skb->len < VLAN_ETH_HLEN))
  206. return 0;
  207. err = __pop_vlan_tci(skb, &tci);
  208. if (err)
  209. return err;
  210. }
  211. /* move next vlan tag to hw accel tag */
  212. if (likely(skb->protocol != htons(ETH_P_8021Q) ||
  213. skb->len < VLAN_ETH_HLEN)) {
  214. key->eth.tci = 0;
  215. return 0;
  216. }
  217. invalidate_flow_key(key);
  218. err = __pop_vlan_tci(skb, &tci);
  219. if (unlikely(err))
  220. return err;
  221. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
  222. return 0;
  223. }
  224. static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
  225. const struct ovs_action_push_vlan *vlan)
  226. {
  227. if (unlikely(vlan_tx_tag_present(skb))) {
  228. u16 current_tag;
  229. /* push down current VLAN tag */
  230. current_tag = vlan_tx_tag_get(skb);
  231. if (!__vlan_put_tag(skb, skb->vlan_proto, current_tag))
  232. return -ENOMEM;
  233. /* Update mac_len for subsequent MPLS actions */
  234. skb->mac_len += VLAN_HLEN;
  235. if (skb->ip_summed == CHECKSUM_COMPLETE)
  236. skb->csum = csum_add(skb->csum, csum_partial(skb->data
  237. + (2 * ETH_ALEN), VLAN_HLEN, 0));
  238. invalidate_flow_key(key);
  239. } else {
  240. key->eth.tci = vlan->vlan_tci;
  241. }
  242. __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  243. return 0;
  244. }
  245. static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *key,
  246. const struct ovs_key_ethernet *eth_key)
  247. {
  248. int err;
  249. err = make_writable(skb, ETH_HLEN);
  250. if (unlikely(err))
  251. return err;
  252. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  253. ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
  254. ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
  255. ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  256. ether_addr_copy(key->eth.src, eth_key->eth_src);
  257. ether_addr_copy(key->eth.dst, eth_key->eth_dst);
  258. return 0;
  259. }
  260. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  261. __be32 *addr, __be32 new_addr)
  262. {
  263. int transport_len = skb->len - skb_transport_offset(skb);
  264. if (nh->protocol == IPPROTO_TCP) {
  265. if (likely(transport_len >= sizeof(struct tcphdr)))
  266. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  267. *addr, new_addr, 1);
  268. } else if (nh->protocol == IPPROTO_UDP) {
  269. if (likely(transport_len >= sizeof(struct udphdr))) {
  270. struct udphdr *uh = udp_hdr(skb);
  271. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  272. inet_proto_csum_replace4(&uh->check, skb,
  273. *addr, new_addr, 1);
  274. if (!uh->check)
  275. uh->check = CSUM_MANGLED_0;
  276. }
  277. }
  278. }
  279. csum_replace4(&nh->check, *addr, new_addr);
  280. skb_clear_hash(skb);
  281. *addr = new_addr;
  282. }
  283. static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
  284. __be32 addr[4], const __be32 new_addr[4])
  285. {
  286. int transport_len = skb->len - skb_transport_offset(skb);
  287. if (l4_proto == IPPROTO_TCP) {
  288. if (likely(transport_len >= sizeof(struct tcphdr)))
  289. inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
  290. addr, new_addr, 1);
  291. } else if (l4_proto == IPPROTO_UDP) {
  292. if (likely(transport_len >= sizeof(struct udphdr))) {
  293. struct udphdr *uh = udp_hdr(skb);
  294. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  295. inet_proto_csum_replace16(&uh->check, skb,
  296. addr, new_addr, 1);
  297. if (!uh->check)
  298. uh->check = CSUM_MANGLED_0;
  299. }
  300. }
  301. }
  302. }
  303. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  304. __be32 addr[4], const __be32 new_addr[4],
  305. bool recalculate_csum)
  306. {
  307. if (recalculate_csum)
  308. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  309. skb_clear_hash(skb);
  310. memcpy(addr, new_addr, sizeof(__be32[4]));
  311. }
  312. static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
  313. {
  314. nh->priority = tc >> 4;
  315. nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
  316. }
  317. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
  318. {
  319. nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
  320. nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
  321. nh->flow_lbl[2] = fl & 0x000000FF;
  322. }
  323. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
  324. {
  325. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  326. nh->ttl = new_ttl;
  327. }
  328. static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *key,
  329. const struct ovs_key_ipv4 *ipv4_key)
  330. {
  331. struct iphdr *nh;
  332. int err;
  333. err = make_writable(skb, skb_network_offset(skb) +
  334. sizeof(struct iphdr));
  335. if (unlikely(err))
  336. return err;
  337. nh = ip_hdr(skb);
  338. if (ipv4_key->ipv4_src != nh->saddr) {
  339. set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
  340. key->ipv4.addr.src = ipv4_key->ipv4_src;
  341. }
  342. if (ipv4_key->ipv4_dst != nh->daddr) {
  343. set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
  344. key->ipv4.addr.dst = ipv4_key->ipv4_dst;
  345. }
  346. if (ipv4_key->ipv4_tos != nh->tos) {
  347. ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
  348. key->ip.tos = nh->tos;
  349. }
  350. if (ipv4_key->ipv4_ttl != nh->ttl) {
  351. set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
  352. key->ip.ttl = ipv4_key->ipv4_ttl;
  353. }
  354. return 0;
  355. }
  356. static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *key,
  357. const struct ovs_key_ipv6 *ipv6_key)
  358. {
  359. struct ipv6hdr *nh;
  360. int err;
  361. __be32 *saddr;
  362. __be32 *daddr;
  363. err = make_writable(skb, skb_network_offset(skb) +
  364. sizeof(struct ipv6hdr));
  365. if (unlikely(err))
  366. return err;
  367. nh = ipv6_hdr(skb);
  368. saddr = (__be32 *)&nh->saddr;
  369. daddr = (__be32 *)&nh->daddr;
  370. if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src))) {
  371. set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
  372. ipv6_key->ipv6_src, true);
  373. memcpy(&key->ipv6.addr.src, ipv6_key->ipv6_src,
  374. sizeof(ipv6_key->ipv6_src));
  375. }
  376. if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
  377. unsigned int offset = 0;
  378. int flags = IP6_FH_F_SKIP_RH;
  379. bool recalc_csum = true;
  380. if (ipv6_ext_hdr(nh->nexthdr))
  381. recalc_csum = ipv6_find_hdr(skb, &offset,
  382. NEXTHDR_ROUTING, NULL,
  383. &flags) != NEXTHDR_ROUTING;
  384. set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
  385. ipv6_key->ipv6_dst, recalc_csum);
  386. memcpy(&key->ipv6.addr.dst, ipv6_key->ipv6_dst,
  387. sizeof(ipv6_key->ipv6_dst));
  388. }
  389. set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
  390. key->ip.tos = ipv6_get_dsfield(nh);
  391. set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
  392. key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  393. nh->hop_limit = ipv6_key->ipv6_hlimit;
  394. key->ip.ttl = ipv6_key->ipv6_hlimit;
  395. return 0;
  396. }
  397. /* Must follow make_writable() since that can move the skb data. */
  398. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  399. __be16 new_port, __sum16 *check)
  400. {
  401. inet_proto_csum_replace2(check, skb, *port, new_port, 0);
  402. *port = new_port;
  403. skb_clear_hash(skb);
  404. }
  405. static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
  406. {
  407. struct udphdr *uh = udp_hdr(skb);
  408. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  409. set_tp_port(skb, port, new_port, &uh->check);
  410. if (!uh->check)
  411. uh->check = CSUM_MANGLED_0;
  412. } else {
  413. *port = new_port;
  414. skb_clear_hash(skb);
  415. }
  416. }
  417. static int set_udp(struct sk_buff *skb, struct sw_flow_key *key,
  418. const struct ovs_key_udp *udp_port_key)
  419. {
  420. struct udphdr *uh;
  421. int err;
  422. err = make_writable(skb, skb_transport_offset(skb) +
  423. sizeof(struct udphdr));
  424. if (unlikely(err))
  425. return err;
  426. uh = udp_hdr(skb);
  427. if (udp_port_key->udp_src != uh->source) {
  428. set_udp_port(skb, &uh->source, udp_port_key->udp_src);
  429. key->tp.src = udp_port_key->udp_src;
  430. }
  431. if (udp_port_key->udp_dst != uh->dest) {
  432. set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
  433. key->tp.dst = udp_port_key->udp_dst;
  434. }
  435. return 0;
  436. }
  437. static int set_tcp(struct sk_buff *skb, struct sw_flow_key *key,
  438. const struct ovs_key_tcp *tcp_port_key)
  439. {
  440. struct tcphdr *th;
  441. int err;
  442. err = make_writable(skb, skb_transport_offset(skb) +
  443. sizeof(struct tcphdr));
  444. if (unlikely(err))
  445. return err;
  446. th = tcp_hdr(skb);
  447. if (tcp_port_key->tcp_src != th->source) {
  448. set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
  449. key->tp.src = tcp_port_key->tcp_src;
  450. }
  451. if (tcp_port_key->tcp_dst != th->dest) {
  452. set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
  453. key->tp.dst = tcp_port_key->tcp_dst;
  454. }
  455. return 0;
  456. }
  457. static int set_sctp(struct sk_buff *skb, struct sw_flow_key *key,
  458. const struct ovs_key_sctp *sctp_port_key)
  459. {
  460. struct sctphdr *sh;
  461. int err;
  462. unsigned int sctphoff = skb_transport_offset(skb);
  463. err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
  464. if (unlikely(err))
  465. return err;
  466. sh = sctp_hdr(skb);
  467. if (sctp_port_key->sctp_src != sh->source ||
  468. sctp_port_key->sctp_dst != sh->dest) {
  469. __le32 old_correct_csum, new_csum, old_csum;
  470. old_csum = sh->checksum;
  471. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  472. sh->source = sctp_port_key->sctp_src;
  473. sh->dest = sctp_port_key->sctp_dst;
  474. new_csum = sctp_compute_cksum(skb, sctphoff);
  475. /* Carry any checksum errors through. */
  476. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  477. skb_clear_hash(skb);
  478. key->tp.src = sctp_port_key->sctp_src;
  479. key->tp.dst = sctp_port_key->sctp_dst;
  480. }
  481. return 0;
  482. }
  483. static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
  484. {
  485. struct vport *vport = ovs_vport_rcu(dp, out_port);
  486. if (likely(vport))
  487. ovs_vport_send(vport, skb);
  488. else
  489. kfree_skb(skb);
  490. }
  491. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  492. struct sw_flow_key *key, const struct nlattr *attr)
  493. {
  494. struct ovs_tunnel_info info;
  495. struct dp_upcall_info upcall;
  496. const struct nlattr *a;
  497. int rem;
  498. upcall.cmd = OVS_PACKET_CMD_ACTION;
  499. upcall.userdata = NULL;
  500. upcall.portid = 0;
  501. upcall.egress_tun_info = NULL;
  502. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  503. a = nla_next(a, &rem)) {
  504. switch (nla_type(a)) {
  505. case OVS_USERSPACE_ATTR_USERDATA:
  506. upcall.userdata = a;
  507. break;
  508. case OVS_USERSPACE_ATTR_PID:
  509. upcall.portid = nla_get_u32(a);
  510. break;
  511. case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
  512. /* Get out tunnel info. */
  513. struct vport *vport;
  514. vport = ovs_vport_rcu(dp, nla_get_u32(a));
  515. if (vport) {
  516. int err;
  517. err = ovs_vport_get_egress_tun_info(vport, skb,
  518. &info);
  519. if (!err)
  520. upcall.egress_tun_info = &info;
  521. }
  522. break;
  523. }
  524. } /* End of switch. */
  525. }
  526. return ovs_dp_upcall(dp, skb, key, &upcall);
  527. }
  528. static int sample(struct datapath *dp, struct sk_buff *skb,
  529. struct sw_flow_key *key, const struct nlattr *attr)
  530. {
  531. const struct nlattr *acts_list = NULL;
  532. const struct nlattr *a;
  533. int rem;
  534. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  535. a = nla_next(a, &rem)) {
  536. switch (nla_type(a)) {
  537. case OVS_SAMPLE_ATTR_PROBABILITY:
  538. if (prandom_u32() >= nla_get_u32(a))
  539. return 0;
  540. break;
  541. case OVS_SAMPLE_ATTR_ACTIONS:
  542. acts_list = a;
  543. break;
  544. }
  545. }
  546. rem = nla_len(acts_list);
  547. a = nla_data(acts_list);
  548. /* Actions list is empty, do nothing */
  549. if (unlikely(!rem))
  550. return 0;
  551. /* The only known usage of sample action is having a single user-space
  552. * action. Treat this usage as a special case.
  553. * The output_userspace() should clone the skb to be sent to the
  554. * user space. This skb will be consumed by its caller.
  555. */
  556. if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
  557. nla_is_last(a, rem)))
  558. return output_userspace(dp, skb, key, a);
  559. skb = skb_clone(skb, GFP_ATOMIC);
  560. if (!skb)
  561. /* Skip the sample action when out of memory. */
  562. return 0;
  563. if (!add_deferred_actions(skb, key, a)) {
  564. if (net_ratelimit())
  565. pr_warn("%s: deferred actions limit reached, dropping sample action\n",
  566. ovs_dp_name(dp));
  567. kfree_skb(skb);
  568. }
  569. return 0;
  570. }
  571. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  572. const struct nlattr *attr)
  573. {
  574. struct ovs_action_hash *hash_act = nla_data(attr);
  575. u32 hash = 0;
  576. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  577. hash = skb_get_hash(skb);
  578. hash = jhash_1word(hash, hash_act->hash_basis);
  579. if (!hash)
  580. hash = 0x1;
  581. key->ovs_flow_hash = hash;
  582. }
  583. static int execute_set_action(struct sk_buff *skb, struct sw_flow_key *key,
  584. const struct nlattr *nested_attr)
  585. {
  586. int err = 0;
  587. switch (nla_type(nested_attr)) {
  588. case OVS_KEY_ATTR_PRIORITY:
  589. skb->priority = nla_get_u32(nested_attr);
  590. key->phy.priority = skb->priority;
  591. break;
  592. case OVS_KEY_ATTR_SKB_MARK:
  593. skb->mark = nla_get_u32(nested_attr);
  594. key->phy.skb_mark = skb->mark;
  595. break;
  596. case OVS_KEY_ATTR_TUNNEL_INFO:
  597. OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
  598. break;
  599. case OVS_KEY_ATTR_ETHERNET:
  600. err = set_eth_addr(skb, key, nla_data(nested_attr));
  601. break;
  602. case OVS_KEY_ATTR_IPV4:
  603. err = set_ipv4(skb, key, nla_data(nested_attr));
  604. break;
  605. case OVS_KEY_ATTR_IPV6:
  606. err = set_ipv6(skb, key, nla_data(nested_attr));
  607. break;
  608. case OVS_KEY_ATTR_TCP:
  609. err = set_tcp(skb, key, nla_data(nested_attr));
  610. break;
  611. case OVS_KEY_ATTR_UDP:
  612. err = set_udp(skb, key, nla_data(nested_attr));
  613. break;
  614. case OVS_KEY_ATTR_SCTP:
  615. err = set_sctp(skb, key, nla_data(nested_attr));
  616. break;
  617. case OVS_KEY_ATTR_MPLS:
  618. err = set_mpls(skb, key, nla_data(nested_attr));
  619. break;
  620. }
  621. return err;
  622. }
  623. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  624. struct sw_flow_key *key,
  625. const struct nlattr *a, int rem)
  626. {
  627. struct deferred_action *da;
  628. if (!is_flow_key_valid(key)) {
  629. int err;
  630. err = ovs_flow_key_update(skb, key);
  631. if (err)
  632. return err;
  633. }
  634. BUG_ON(!is_flow_key_valid(key));
  635. if (!nla_is_last(a, rem)) {
  636. /* Recirc action is the not the last action
  637. * of the action list, need to clone the skb.
  638. */
  639. skb = skb_clone(skb, GFP_ATOMIC);
  640. /* Skip the recirc action when out of memory, but
  641. * continue on with the rest of the action list.
  642. */
  643. if (!skb)
  644. return 0;
  645. }
  646. da = add_deferred_actions(skb, key, NULL);
  647. if (da) {
  648. da->pkt_key.recirc_id = nla_get_u32(a);
  649. } else {
  650. kfree_skb(skb);
  651. if (net_ratelimit())
  652. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  653. ovs_dp_name(dp));
  654. }
  655. return 0;
  656. }
  657. /* Execute a list of actions against 'skb'. */
  658. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  659. struct sw_flow_key *key,
  660. const struct nlattr *attr, int len)
  661. {
  662. /* Every output action needs a separate clone of 'skb', but the common
  663. * case is just a single output action, so that doing a clone and
  664. * then freeing the original skbuff is wasteful. So the following code
  665. * is slightly obscure just to avoid that.
  666. */
  667. int prev_port = -1;
  668. const struct nlattr *a;
  669. int rem;
  670. for (a = attr, rem = len; rem > 0;
  671. a = nla_next(a, &rem)) {
  672. int err = 0;
  673. if (unlikely(prev_port != -1)) {
  674. struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
  675. if (out_skb)
  676. do_output(dp, out_skb, prev_port);
  677. prev_port = -1;
  678. }
  679. switch (nla_type(a)) {
  680. case OVS_ACTION_ATTR_OUTPUT:
  681. prev_port = nla_get_u32(a);
  682. break;
  683. case OVS_ACTION_ATTR_USERSPACE:
  684. output_userspace(dp, skb, key, a);
  685. break;
  686. case OVS_ACTION_ATTR_HASH:
  687. execute_hash(skb, key, a);
  688. break;
  689. case OVS_ACTION_ATTR_PUSH_MPLS:
  690. err = push_mpls(skb, key, nla_data(a));
  691. break;
  692. case OVS_ACTION_ATTR_POP_MPLS:
  693. err = pop_mpls(skb, key, nla_get_be16(a));
  694. break;
  695. case OVS_ACTION_ATTR_PUSH_VLAN:
  696. err = push_vlan(skb, key, nla_data(a));
  697. if (unlikely(err)) /* skb already freed. */
  698. return err;
  699. break;
  700. case OVS_ACTION_ATTR_POP_VLAN:
  701. err = pop_vlan(skb, key);
  702. break;
  703. case OVS_ACTION_ATTR_RECIRC:
  704. err = execute_recirc(dp, skb, key, a, rem);
  705. if (nla_is_last(a, rem)) {
  706. /* If this is the last action, the skb has
  707. * been consumed or freed.
  708. * Return immediately.
  709. */
  710. return err;
  711. }
  712. break;
  713. case OVS_ACTION_ATTR_SET:
  714. err = execute_set_action(skb, key, nla_data(a));
  715. break;
  716. case OVS_ACTION_ATTR_SAMPLE:
  717. err = sample(dp, skb, key, a);
  718. if (unlikely(err)) /* skb already freed. */
  719. return err;
  720. break;
  721. }
  722. if (unlikely(err)) {
  723. kfree_skb(skb);
  724. return err;
  725. }
  726. }
  727. if (prev_port != -1)
  728. do_output(dp, skb, prev_port);
  729. else
  730. consume_skb(skb);
  731. return 0;
  732. }
  733. static void process_deferred_actions(struct datapath *dp)
  734. {
  735. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  736. /* Do not touch the FIFO in case there is no deferred actions. */
  737. if (action_fifo_is_empty(fifo))
  738. return;
  739. /* Finishing executing all deferred actions. */
  740. do {
  741. struct deferred_action *da = action_fifo_get(fifo);
  742. struct sk_buff *skb = da->skb;
  743. struct sw_flow_key *key = &da->pkt_key;
  744. const struct nlattr *actions = da->actions;
  745. if (actions)
  746. do_execute_actions(dp, skb, key, actions,
  747. nla_len(actions));
  748. else
  749. ovs_dp_process_packet(skb, key);
  750. } while (!action_fifo_is_empty(fifo));
  751. /* Reset FIFO for the next packet. */
  752. action_fifo_init(fifo);
  753. }
  754. /* Execute a list of actions against 'skb'. */
  755. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  756. const struct sw_flow_actions *acts,
  757. struct sw_flow_key *key)
  758. {
  759. int level = this_cpu_read(exec_actions_level);
  760. int err;
  761. this_cpu_inc(exec_actions_level);
  762. OVS_CB(skb)->egress_tun_info = NULL;
  763. err = do_execute_actions(dp, skb, key,
  764. acts->actions, acts->actions_len);
  765. if (!level)
  766. process_deferred_actions(dp);
  767. this_cpu_dec(exec_actions_level);
  768. return err;
  769. }
  770. int action_fifos_init(void)
  771. {
  772. action_fifos = alloc_percpu(struct action_fifo);
  773. if (!action_fifos)
  774. return -ENOMEM;
  775. return 0;
  776. }
  777. void action_fifos_exit(void)
  778. {
  779. free_percpu(action_fifos);
  780. }