actions.c 21 KB

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