actions.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 == NEXTHDR_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 == NEXTHDR_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. } else if (l4_proto == NEXTHDR_ICMP) {
  216. if (likely(transport_len >= sizeof(struct icmp6hdr)))
  217. inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
  218. skb, addr, new_addr, 1);
  219. }
  220. }
  221. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  222. __be32 addr[4], const __be32 new_addr[4],
  223. bool recalculate_csum)
  224. {
  225. if (recalculate_csum)
  226. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  227. skb_clear_hash(skb);
  228. memcpy(addr, new_addr, sizeof(__be32[4]));
  229. }
  230. static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
  231. {
  232. nh->priority = tc >> 4;
  233. nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
  234. }
  235. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
  236. {
  237. nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
  238. nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
  239. nh->flow_lbl[2] = fl & 0x000000FF;
  240. }
  241. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
  242. {
  243. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  244. nh->ttl = new_ttl;
  245. }
  246. static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
  247. {
  248. struct iphdr *nh;
  249. int err;
  250. err = make_writable(skb, skb_network_offset(skb) +
  251. sizeof(struct iphdr));
  252. if (unlikely(err))
  253. return err;
  254. nh = ip_hdr(skb);
  255. if (ipv4_key->ipv4_src != nh->saddr)
  256. set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
  257. if (ipv4_key->ipv4_dst != nh->daddr)
  258. set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
  259. if (ipv4_key->ipv4_tos != nh->tos)
  260. ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
  261. if (ipv4_key->ipv4_ttl != nh->ttl)
  262. set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
  263. return 0;
  264. }
  265. static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
  266. {
  267. struct ipv6hdr *nh;
  268. int err;
  269. __be32 *saddr;
  270. __be32 *daddr;
  271. err = make_writable(skb, skb_network_offset(skb) +
  272. sizeof(struct ipv6hdr));
  273. if (unlikely(err))
  274. return err;
  275. nh = ipv6_hdr(skb);
  276. saddr = (__be32 *)&nh->saddr;
  277. daddr = (__be32 *)&nh->daddr;
  278. if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
  279. set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
  280. ipv6_key->ipv6_src, true);
  281. if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
  282. unsigned int offset = 0;
  283. int flags = IP6_FH_F_SKIP_RH;
  284. bool recalc_csum = true;
  285. if (ipv6_ext_hdr(nh->nexthdr))
  286. recalc_csum = ipv6_find_hdr(skb, &offset,
  287. NEXTHDR_ROUTING, NULL,
  288. &flags) != NEXTHDR_ROUTING;
  289. set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
  290. ipv6_key->ipv6_dst, recalc_csum);
  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. return 0;
  296. }
  297. /* Must follow make_writable() since that can move the skb data. */
  298. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  299. __be16 new_port, __sum16 *check)
  300. {
  301. inet_proto_csum_replace2(check, skb, *port, new_port, 0);
  302. *port = new_port;
  303. skb_clear_hash(skb);
  304. }
  305. static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
  306. {
  307. struct udphdr *uh = udp_hdr(skb);
  308. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  309. set_tp_port(skb, port, new_port, &uh->check);
  310. if (!uh->check)
  311. uh->check = CSUM_MANGLED_0;
  312. } else {
  313. *port = new_port;
  314. skb_clear_hash(skb);
  315. }
  316. }
  317. static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
  318. {
  319. struct udphdr *uh;
  320. int err;
  321. err = make_writable(skb, skb_transport_offset(skb) +
  322. sizeof(struct udphdr));
  323. if (unlikely(err))
  324. return err;
  325. uh = udp_hdr(skb);
  326. if (udp_port_key->udp_src != uh->source)
  327. set_udp_port(skb, &uh->source, udp_port_key->udp_src);
  328. if (udp_port_key->udp_dst != uh->dest)
  329. set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
  330. return 0;
  331. }
  332. static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
  333. {
  334. struct tcphdr *th;
  335. int err;
  336. err = make_writable(skb, skb_transport_offset(skb) +
  337. sizeof(struct tcphdr));
  338. if (unlikely(err))
  339. return err;
  340. th = tcp_hdr(skb);
  341. if (tcp_port_key->tcp_src != th->source)
  342. set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
  343. if (tcp_port_key->tcp_dst != th->dest)
  344. set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
  345. return 0;
  346. }
  347. static int set_sctp(struct sk_buff *skb,
  348. const struct ovs_key_sctp *sctp_port_key)
  349. {
  350. struct sctphdr *sh;
  351. int err;
  352. unsigned int sctphoff = skb_transport_offset(skb);
  353. err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
  354. if (unlikely(err))
  355. return err;
  356. sh = sctp_hdr(skb);
  357. if (sctp_port_key->sctp_src != sh->source ||
  358. sctp_port_key->sctp_dst != sh->dest) {
  359. __le32 old_correct_csum, new_csum, old_csum;
  360. old_csum = sh->checksum;
  361. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  362. sh->source = sctp_port_key->sctp_src;
  363. sh->dest = sctp_port_key->sctp_dst;
  364. new_csum = sctp_compute_cksum(skb, sctphoff);
  365. /* Carry any checksum errors through. */
  366. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  367. skb_clear_hash(skb);
  368. }
  369. return 0;
  370. }
  371. static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
  372. {
  373. struct vport *vport;
  374. if (unlikely(!skb))
  375. return -ENOMEM;
  376. vport = ovs_vport_rcu(dp, out_port);
  377. if (unlikely(!vport)) {
  378. kfree_skb(skb);
  379. return -ENODEV;
  380. }
  381. ovs_vport_send(vport, skb);
  382. return 0;
  383. }
  384. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  385. struct sw_flow_key *key, const struct nlattr *attr)
  386. {
  387. struct dp_upcall_info upcall;
  388. const struct nlattr *a;
  389. int rem;
  390. upcall.cmd = OVS_PACKET_CMD_ACTION;
  391. upcall.key = key;
  392. upcall.userdata = NULL;
  393. upcall.portid = 0;
  394. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  395. a = nla_next(a, &rem)) {
  396. switch (nla_type(a)) {
  397. case OVS_USERSPACE_ATTR_USERDATA:
  398. upcall.userdata = a;
  399. break;
  400. case OVS_USERSPACE_ATTR_PID:
  401. upcall.portid = nla_get_u32(a);
  402. break;
  403. }
  404. }
  405. return ovs_dp_upcall(dp, skb, &upcall);
  406. }
  407. static bool last_action(const struct nlattr *a, int rem)
  408. {
  409. return a->nla_len == rem;
  410. }
  411. static int sample(struct datapath *dp, struct sk_buff *skb,
  412. struct sw_flow_key *key, const struct nlattr *attr)
  413. {
  414. const struct nlattr *acts_list = NULL;
  415. const struct nlattr *a;
  416. int rem;
  417. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  418. a = nla_next(a, &rem)) {
  419. switch (nla_type(a)) {
  420. case OVS_SAMPLE_ATTR_PROBABILITY:
  421. if (prandom_u32() >= nla_get_u32(a))
  422. return 0;
  423. break;
  424. case OVS_SAMPLE_ATTR_ACTIONS:
  425. acts_list = a;
  426. break;
  427. }
  428. }
  429. rem = nla_len(acts_list);
  430. a = nla_data(acts_list);
  431. /* Actions list is empty, do nothing */
  432. if (unlikely(!rem))
  433. return 0;
  434. /* The only known usage of sample action is having a single user-space
  435. * action. Treat this usage as a special case.
  436. * The output_userspace() should clone the skb to be sent to the
  437. * user space. This skb will be consumed by its caller.
  438. */
  439. if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
  440. last_action(a, rem)))
  441. return output_userspace(dp, skb, key, a);
  442. skb = skb_clone(skb, GFP_ATOMIC);
  443. if (!skb)
  444. /* Skip the sample action when out of memory. */
  445. return 0;
  446. if (!add_deferred_actions(skb, key, a)) {
  447. if (net_ratelimit())
  448. pr_warn("%s: deferred actions limit reached, dropping sample action\n",
  449. ovs_dp_name(dp));
  450. kfree_skb(skb);
  451. }
  452. return 0;
  453. }
  454. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  455. const struct nlattr *attr)
  456. {
  457. struct ovs_action_hash *hash_act = nla_data(attr);
  458. u32 hash = 0;
  459. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  460. hash = skb_get_hash(skb);
  461. hash = jhash_1word(hash, hash_act->hash_basis);
  462. if (!hash)
  463. hash = 0x1;
  464. key->ovs_flow_hash = hash;
  465. }
  466. static int execute_set_action(struct sk_buff *skb,
  467. const struct nlattr *nested_attr)
  468. {
  469. int err = 0;
  470. switch (nla_type(nested_attr)) {
  471. case OVS_KEY_ATTR_PRIORITY:
  472. skb->priority = nla_get_u32(nested_attr);
  473. break;
  474. case OVS_KEY_ATTR_SKB_MARK:
  475. skb->mark = nla_get_u32(nested_attr);
  476. break;
  477. case OVS_KEY_ATTR_TUNNEL_INFO:
  478. OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
  479. break;
  480. case OVS_KEY_ATTR_ETHERNET:
  481. err = set_eth_addr(skb, nla_data(nested_attr));
  482. break;
  483. case OVS_KEY_ATTR_IPV4:
  484. err = set_ipv4(skb, nla_data(nested_attr));
  485. break;
  486. case OVS_KEY_ATTR_IPV6:
  487. err = set_ipv6(skb, nla_data(nested_attr));
  488. break;
  489. case OVS_KEY_ATTR_TCP:
  490. err = set_tcp(skb, nla_data(nested_attr));
  491. break;
  492. case OVS_KEY_ATTR_UDP:
  493. err = set_udp(skb, nla_data(nested_attr));
  494. break;
  495. case OVS_KEY_ATTR_SCTP:
  496. err = set_sctp(skb, nla_data(nested_attr));
  497. break;
  498. }
  499. return err;
  500. }
  501. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  502. struct sw_flow_key *key,
  503. const struct nlattr *a, int rem)
  504. {
  505. struct deferred_action *da;
  506. int err;
  507. err = ovs_flow_key_update(skb, key);
  508. if (err)
  509. return err;
  510. if (!last_action(a, rem)) {
  511. /* Recirc action is the not the last action
  512. * of the action list, need to clone the skb.
  513. */
  514. skb = skb_clone(skb, GFP_ATOMIC);
  515. /* Skip the recirc action when out of memory, but
  516. * continue on with the rest of the action list.
  517. */
  518. if (!skb)
  519. return 0;
  520. }
  521. da = add_deferred_actions(skb, key, NULL);
  522. if (da) {
  523. da->pkt_key.recirc_id = nla_get_u32(a);
  524. } else {
  525. kfree_skb(skb);
  526. if (net_ratelimit())
  527. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  528. ovs_dp_name(dp));
  529. }
  530. return 0;
  531. }
  532. /* Execute a list of actions against 'skb'. */
  533. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  534. struct sw_flow_key *key,
  535. const struct nlattr *attr, int len)
  536. {
  537. /* Every output action needs a separate clone of 'skb', but the common
  538. * case is just a single output action, so that doing a clone and
  539. * then freeing the original skbuff is wasteful. So the following code
  540. * is slightly obscure just to avoid that. */
  541. int prev_port = -1;
  542. const struct nlattr *a;
  543. int rem;
  544. for (a = attr, rem = len; rem > 0;
  545. a = nla_next(a, &rem)) {
  546. int err = 0;
  547. if (prev_port != -1) {
  548. do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
  549. prev_port = -1;
  550. }
  551. switch (nla_type(a)) {
  552. case OVS_ACTION_ATTR_OUTPUT:
  553. prev_port = nla_get_u32(a);
  554. break;
  555. case OVS_ACTION_ATTR_USERSPACE:
  556. output_userspace(dp, skb, key, a);
  557. break;
  558. case OVS_ACTION_ATTR_HASH:
  559. execute_hash(skb, key, a);
  560. break;
  561. case OVS_ACTION_ATTR_PUSH_VLAN:
  562. err = push_vlan(skb, nla_data(a));
  563. if (unlikely(err)) /* skb already freed. */
  564. return err;
  565. break;
  566. case OVS_ACTION_ATTR_POP_VLAN:
  567. err = pop_vlan(skb);
  568. break;
  569. case OVS_ACTION_ATTR_RECIRC:
  570. err = execute_recirc(dp, skb, key, a, rem);
  571. if (last_action(a, rem)) {
  572. /* If this is the last action, the skb has
  573. * been consumed or freed.
  574. * Return immediately.
  575. */
  576. return err;
  577. }
  578. break;
  579. case OVS_ACTION_ATTR_SET:
  580. err = execute_set_action(skb, nla_data(a));
  581. break;
  582. case OVS_ACTION_ATTR_SAMPLE:
  583. err = sample(dp, skb, key, a);
  584. break;
  585. }
  586. if (unlikely(err)) {
  587. kfree_skb(skb);
  588. return err;
  589. }
  590. }
  591. if (prev_port != -1)
  592. do_output(dp, skb, prev_port);
  593. else
  594. consume_skb(skb);
  595. return 0;
  596. }
  597. static void process_deferred_actions(struct datapath *dp)
  598. {
  599. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  600. /* Do not touch the FIFO in case there is no deferred actions. */
  601. if (action_fifo_is_empty(fifo))
  602. return;
  603. /* Finishing executing all deferred actions. */
  604. do {
  605. struct deferred_action *da = action_fifo_get(fifo);
  606. struct sk_buff *skb = da->skb;
  607. struct sw_flow_key *key = &da->pkt_key;
  608. const struct nlattr *actions = da->actions;
  609. if (actions)
  610. do_execute_actions(dp, skb, key, actions,
  611. nla_len(actions));
  612. else
  613. ovs_dp_process_packet(skb, key);
  614. } while (!action_fifo_is_empty(fifo));
  615. /* Reset FIFO for the next packet. */
  616. action_fifo_init(fifo);
  617. }
  618. /* Execute a list of actions against 'skb'. */
  619. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  620. struct sw_flow_key *key)
  621. {
  622. int level = this_cpu_read(exec_actions_level);
  623. struct sw_flow_actions *acts;
  624. int err;
  625. acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
  626. this_cpu_inc(exec_actions_level);
  627. OVS_CB(skb)->egress_tun_info = NULL;
  628. err = do_execute_actions(dp, skb, key,
  629. acts->actions, acts->actions_len);
  630. if (!level)
  631. process_deferred_actions(dp);
  632. this_cpu_dec(exec_actions_level);
  633. return err;
  634. }
  635. int action_fifos_init(void)
  636. {
  637. action_fifos = alloc_percpu(struct action_fifo);
  638. if (!action_fifos)
  639. return -ENOMEM;
  640. return 0;
  641. }
  642. void action_fifos_exit(void)
  643. {
  644. free_percpu(action_fifos);
  645. }