actions.c 19 KB

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