actions.c 19 KB

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