actions.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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/netfilter_ipv6.h>
  24. #include <linux/sctp.h>
  25. #include <linux/tcp.h>
  26. #include <linux/udp.h>
  27. #include <linux/in6.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_vlan.h>
  30. #include <net/dst.h>
  31. #include <net/ip.h>
  32. #include <net/ipv6.h>
  33. #include <net/ip6_fib.h>
  34. #include <net/checksum.h>
  35. #include <net/dsfield.h>
  36. #include <net/mpls.h>
  37. #include <net/sctp/checksum.h>
  38. #include "datapath.h"
  39. #include "flow.h"
  40. #include "conntrack.h"
  41. #include "vport.h"
  42. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  43. struct sw_flow_key *key,
  44. const struct nlattr *attr, int len);
  45. struct deferred_action {
  46. struct sk_buff *skb;
  47. const struct nlattr *actions;
  48. /* Store pkt_key clone when creating deferred action. */
  49. struct sw_flow_key pkt_key;
  50. };
  51. #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
  52. struct ovs_frag_data {
  53. unsigned long dst;
  54. struct vport *vport;
  55. struct ovs_skb_cb cb;
  56. __be16 inner_protocol;
  57. u16 network_offset; /* valid only for MPLS */
  58. u16 vlan_tci;
  59. __be16 vlan_proto;
  60. unsigned int l2_len;
  61. u8 mac_proto;
  62. u8 l2_data[MAX_L2_LEN];
  63. };
  64. static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
  65. #define DEFERRED_ACTION_FIFO_SIZE 10
  66. #define OVS_RECURSION_LIMIT 5
  67. #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)
  68. struct action_fifo {
  69. int head;
  70. int tail;
  71. /* Deferred action fifo queue storage. */
  72. struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
  73. };
  74. struct recirc_keys {
  75. struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];
  76. };
  77. static struct action_fifo __percpu *action_fifos;
  78. static struct recirc_keys __percpu *recirc_keys;
  79. static DEFINE_PER_CPU(int, exec_actions_level);
  80. static void action_fifo_init(struct action_fifo *fifo)
  81. {
  82. fifo->head = 0;
  83. fifo->tail = 0;
  84. }
  85. static bool action_fifo_is_empty(const struct action_fifo *fifo)
  86. {
  87. return (fifo->head == fifo->tail);
  88. }
  89. static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
  90. {
  91. if (action_fifo_is_empty(fifo))
  92. return NULL;
  93. return &fifo->fifo[fifo->tail++];
  94. }
  95. static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
  96. {
  97. if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
  98. return NULL;
  99. return &fifo->fifo[fifo->head++];
  100. }
  101. /* Return true if fifo is not full */
  102. static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
  103. const struct sw_flow_key *key,
  104. const struct nlattr *attr)
  105. {
  106. struct action_fifo *fifo;
  107. struct deferred_action *da;
  108. fifo = this_cpu_ptr(action_fifos);
  109. da = action_fifo_put(fifo);
  110. if (da) {
  111. da->skb = skb;
  112. da->actions = attr;
  113. da->pkt_key = *key;
  114. }
  115. return da;
  116. }
  117. static void invalidate_flow_key(struct sw_flow_key *key)
  118. {
  119. key->mac_proto |= SW_FLOW_KEY_INVALID;
  120. }
  121. static bool is_flow_key_valid(const struct sw_flow_key *key)
  122. {
  123. return !(key->mac_proto & SW_FLOW_KEY_INVALID);
  124. }
  125. static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
  126. __be16 ethertype)
  127. {
  128. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  129. __be16 diff[] = { ~(hdr->h_proto), ethertype };
  130. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  131. ~skb->csum);
  132. }
  133. hdr->h_proto = ethertype;
  134. }
  135. static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  136. const struct ovs_action_push_mpls *mpls)
  137. {
  138. struct mpls_shim_hdr *new_mpls_lse;
  139. /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
  140. if (skb->encapsulation)
  141. return -ENOTSUPP;
  142. if (skb_cow_head(skb, MPLS_HLEN) < 0)
  143. return -ENOMEM;
  144. if (!skb->inner_protocol) {
  145. skb_set_inner_network_header(skb, skb->mac_len);
  146. skb_set_inner_protocol(skb, skb->protocol);
  147. }
  148. skb_push(skb, MPLS_HLEN);
  149. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  150. skb->mac_len);
  151. skb_reset_mac_header(skb);
  152. skb_set_network_header(skb, skb->mac_len);
  153. new_mpls_lse = mpls_hdr(skb);
  154. new_mpls_lse->label_stack_entry = mpls->mpls_lse;
  155. skb_postpush_rcsum(skb, new_mpls_lse, MPLS_HLEN);
  156. if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET)
  157. update_ethertype(skb, eth_hdr(skb), mpls->mpls_ethertype);
  158. skb->protocol = mpls->mpls_ethertype;
  159. invalidate_flow_key(key);
  160. return 0;
  161. }
  162. static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
  163. const __be16 ethertype)
  164. {
  165. int err;
  166. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  167. if (unlikely(err))
  168. return err;
  169. skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
  170. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  171. skb->mac_len);
  172. __skb_pull(skb, MPLS_HLEN);
  173. skb_reset_mac_header(skb);
  174. skb_set_network_header(skb, skb->mac_len);
  175. if (ovs_key_mac_proto(key) == MAC_PROTO_ETHERNET) {
  176. struct ethhdr *hdr;
  177. /* mpls_hdr() is used to locate the ethertype field correctly in the
  178. * presence of VLAN tags.
  179. */
  180. hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
  181. update_ethertype(skb, hdr, ethertype);
  182. }
  183. if (eth_p_mpls(skb->protocol))
  184. skb->protocol = ethertype;
  185. invalidate_flow_key(key);
  186. return 0;
  187. }
  188. static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
  189. const __be32 *mpls_lse, const __be32 *mask)
  190. {
  191. struct mpls_shim_hdr *stack;
  192. __be32 lse;
  193. int err;
  194. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  195. if (unlikely(err))
  196. return err;
  197. stack = mpls_hdr(skb);
  198. lse = OVS_MASKED(stack->label_stack_entry, *mpls_lse, *mask);
  199. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  200. __be32 diff[] = { ~(stack->label_stack_entry), lse };
  201. skb->csum = ~csum_partial((char *)diff, sizeof(diff),
  202. ~skb->csum);
  203. }
  204. stack->label_stack_entry = lse;
  205. flow_key->mpls.top_lse = lse;
  206. return 0;
  207. }
  208. static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
  209. {
  210. int err;
  211. err = skb_vlan_pop(skb);
  212. if (skb_vlan_tag_present(skb)) {
  213. invalidate_flow_key(key);
  214. } else {
  215. key->eth.vlan.tci = 0;
  216. key->eth.vlan.tpid = 0;
  217. }
  218. return err;
  219. }
  220. static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
  221. const struct ovs_action_push_vlan *vlan)
  222. {
  223. if (skb_vlan_tag_present(skb)) {
  224. invalidate_flow_key(key);
  225. } else {
  226. key->eth.vlan.tci = vlan->vlan_tci;
  227. key->eth.vlan.tpid = vlan->vlan_tpid;
  228. }
  229. return skb_vlan_push(skb, vlan->vlan_tpid,
  230. ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
  231. }
  232. /* 'src' is already properly masked. */
  233. static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
  234. {
  235. u16 *dst = (u16 *)dst_;
  236. const u16 *src = (const u16 *)src_;
  237. const u16 *mask = (const u16 *)mask_;
  238. OVS_SET_MASKED(dst[0], src[0], mask[0]);
  239. OVS_SET_MASKED(dst[1], src[1], mask[1]);
  240. OVS_SET_MASKED(dst[2], src[2], mask[2]);
  241. }
  242. static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
  243. const struct ovs_key_ethernet *key,
  244. const struct ovs_key_ethernet *mask)
  245. {
  246. int err;
  247. err = skb_ensure_writable(skb, ETH_HLEN);
  248. if (unlikely(err))
  249. return err;
  250. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  251. ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
  252. mask->eth_src);
  253. ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
  254. mask->eth_dst);
  255. skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
  256. ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
  257. ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
  258. return 0;
  259. }
  260. /* pop_eth does not support VLAN packets as this action is never called
  261. * for them.
  262. */
  263. static int pop_eth(struct sk_buff *skb, struct sw_flow_key *key)
  264. {
  265. skb_pull_rcsum(skb, ETH_HLEN);
  266. skb_reset_mac_header(skb);
  267. skb_reset_mac_len(skb);
  268. /* safe right before invalidate_flow_key */
  269. key->mac_proto = MAC_PROTO_NONE;
  270. invalidate_flow_key(key);
  271. return 0;
  272. }
  273. static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
  274. const struct ovs_action_push_eth *ethh)
  275. {
  276. struct ethhdr *hdr;
  277. /* Add the new Ethernet header */
  278. if (skb_cow_head(skb, ETH_HLEN) < 0)
  279. return -ENOMEM;
  280. skb_push(skb, ETH_HLEN);
  281. skb_reset_mac_header(skb);
  282. skb_reset_mac_len(skb);
  283. hdr = eth_hdr(skb);
  284. ether_addr_copy(hdr->h_source, ethh->addresses.eth_src);
  285. ether_addr_copy(hdr->h_dest, ethh->addresses.eth_dst);
  286. hdr->h_proto = skb->protocol;
  287. skb_postpush_rcsum(skb, hdr, ETH_HLEN);
  288. /* safe right before invalidate_flow_key */
  289. key->mac_proto = MAC_PROTO_ETHERNET;
  290. invalidate_flow_key(key);
  291. return 0;
  292. }
  293. static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
  294. __be32 addr, __be32 new_addr)
  295. {
  296. int transport_len = skb->len - skb_transport_offset(skb);
  297. if (nh->frag_off & htons(IP_OFFSET))
  298. return;
  299. if (nh->protocol == IPPROTO_TCP) {
  300. if (likely(transport_len >= sizeof(struct tcphdr)))
  301. inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
  302. addr, new_addr, true);
  303. } else if (nh->protocol == IPPROTO_UDP) {
  304. if (likely(transport_len >= sizeof(struct udphdr))) {
  305. struct udphdr *uh = udp_hdr(skb);
  306. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  307. inet_proto_csum_replace4(&uh->check, skb,
  308. addr, new_addr, true);
  309. if (!uh->check)
  310. uh->check = CSUM_MANGLED_0;
  311. }
  312. }
  313. }
  314. }
  315. static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
  316. __be32 *addr, __be32 new_addr)
  317. {
  318. update_ip_l4_checksum(skb, nh, *addr, new_addr);
  319. csum_replace4(&nh->check, *addr, new_addr);
  320. skb_clear_hash(skb);
  321. *addr = new_addr;
  322. }
  323. static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
  324. __be32 addr[4], const __be32 new_addr[4])
  325. {
  326. int transport_len = skb->len - skb_transport_offset(skb);
  327. if (l4_proto == NEXTHDR_TCP) {
  328. if (likely(transport_len >= sizeof(struct tcphdr)))
  329. inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
  330. addr, new_addr, true);
  331. } else if (l4_proto == NEXTHDR_UDP) {
  332. if (likely(transport_len >= sizeof(struct udphdr))) {
  333. struct udphdr *uh = udp_hdr(skb);
  334. if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
  335. inet_proto_csum_replace16(&uh->check, skb,
  336. addr, new_addr, true);
  337. if (!uh->check)
  338. uh->check = CSUM_MANGLED_0;
  339. }
  340. }
  341. } else if (l4_proto == NEXTHDR_ICMP) {
  342. if (likely(transport_len >= sizeof(struct icmp6hdr)))
  343. inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
  344. skb, addr, new_addr, true);
  345. }
  346. }
  347. static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
  348. const __be32 mask[4], __be32 masked[4])
  349. {
  350. masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
  351. masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
  352. masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
  353. masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
  354. }
  355. static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
  356. __be32 addr[4], const __be32 new_addr[4],
  357. bool recalculate_csum)
  358. {
  359. if (recalculate_csum)
  360. update_ipv6_checksum(skb, l4_proto, addr, new_addr);
  361. skb_clear_hash(skb);
  362. memcpy(addr, new_addr, sizeof(__be32[4]));
  363. }
  364. static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
  365. {
  366. /* Bits 21-24 are always unmasked, so this retains their values. */
  367. OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
  368. OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
  369. OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
  370. }
  371. static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
  372. u8 mask)
  373. {
  374. new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
  375. csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
  376. nh->ttl = new_ttl;
  377. }
  378. static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
  379. const struct ovs_key_ipv4 *key,
  380. const struct ovs_key_ipv4 *mask)
  381. {
  382. struct iphdr *nh;
  383. __be32 new_addr;
  384. int err;
  385. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  386. sizeof(struct iphdr));
  387. if (unlikely(err))
  388. return err;
  389. nh = ip_hdr(skb);
  390. /* Setting an IP addresses is typically only a side effect of
  391. * matching on them in the current userspace implementation, so it
  392. * makes sense to check if the value actually changed.
  393. */
  394. if (mask->ipv4_src) {
  395. new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
  396. if (unlikely(new_addr != nh->saddr)) {
  397. set_ip_addr(skb, nh, &nh->saddr, new_addr);
  398. flow_key->ipv4.addr.src = new_addr;
  399. }
  400. }
  401. if (mask->ipv4_dst) {
  402. new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
  403. if (unlikely(new_addr != nh->daddr)) {
  404. set_ip_addr(skb, nh, &nh->daddr, new_addr);
  405. flow_key->ipv4.addr.dst = new_addr;
  406. }
  407. }
  408. if (mask->ipv4_tos) {
  409. ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
  410. flow_key->ip.tos = nh->tos;
  411. }
  412. if (mask->ipv4_ttl) {
  413. set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
  414. flow_key->ip.ttl = nh->ttl;
  415. }
  416. return 0;
  417. }
  418. static bool is_ipv6_mask_nonzero(const __be32 addr[4])
  419. {
  420. return !!(addr[0] | addr[1] | addr[2] | addr[3]);
  421. }
  422. static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
  423. const struct ovs_key_ipv6 *key,
  424. const struct ovs_key_ipv6 *mask)
  425. {
  426. struct ipv6hdr *nh;
  427. int err;
  428. err = skb_ensure_writable(skb, skb_network_offset(skb) +
  429. sizeof(struct ipv6hdr));
  430. if (unlikely(err))
  431. return err;
  432. nh = ipv6_hdr(skb);
  433. /* Setting an IP addresses is typically only a side effect of
  434. * matching on them in the current userspace implementation, so it
  435. * makes sense to check if the value actually changed.
  436. */
  437. if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
  438. __be32 *saddr = (__be32 *)&nh->saddr;
  439. __be32 masked[4];
  440. mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
  441. if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
  442. set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
  443. true);
  444. memcpy(&flow_key->ipv6.addr.src, masked,
  445. sizeof(flow_key->ipv6.addr.src));
  446. }
  447. }
  448. if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
  449. unsigned int offset = 0;
  450. int flags = IP6_FH_F_SKIP_RH;
  451. bool recalc_csum = true;
  452. __be32 *daddr = (__be32 *)&nh->daddr;
  453. __be32 masked[4];
  454. mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
  455. if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
  456. if (ipv6_ext_hdr(nh->nexthdr))
  457. recalc_csum = (ipv6_find_hdr(skb, &offset,
  458. NEXTHDR_ROUTING,
  459. NULL, &flags)
  460. != NEXTHDR_ROUTING);
  461. set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
  462. recalc_csum);
  463. memcpy(&flow_key->ipv6.addr.dst, masked,
  464. sizeof(flow_key->ipv6.addr.dst));
  465. }
  466. }
  467. if (mask->ipv6_tclass) {
  468. ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
  469. flow_key->ip.tos = ipv6_get_dsfield(nh);
  470. }
  471. if (mask->ipv6_label) {
  472. set_ipv6_fl(nh, ntohl(key->ipv6_label),
  473. ntohl(mask->ipv6_label));
  474. flow_key->ipv6.label =
  475. *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
  476. }
  477. if (mask->ipv6_hlimit) {
  478. OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
  479. mask->ipv6_hlimit);
  480. flow_key->ip.ttl = nh->hop_limit;
  481. }
  482. return 0;
  483. }
  484. /* Must follow skb_ensure_writable() since that can move the skb data. */
  485. static void set_tp_port(struct sk_buff *skb, __be16 *port,
  486. __be16 new_port, __sum16 *check)
  487. {
  488. inet_proto_csum_replace2(check, skb, *port, new_port, false);
  489. *port = new_port;
  490. }
  491. static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  492. const struct ovs_key_udp *key,
  493. const struct ovs_key_udp *mask)
  494. {
  495. struct udphdr *uh;
  496. __be16 src, dst;
  497. int err;
  498. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  499. sizeof(struct udphdr));
  500. if (unlikely(err))
  501. return err;
  502. uh = udp_hdr(skb);
  503. /* Either of the masks is non-zero, so do not bother checking them. */
  504. src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
  505. dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
  506. if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
  507. if (likely(src != uh->source)) {
  508. set_tp_port(skb, &uh->source, src, &uh->check);
  509. flow_key->tp.src = src;
  510. }
  511. if (likely(dst != uh->dest)) {
  512. set_tp_port(skb, &uh->dest, dst, &uh->check);
  513. flow_key->tp.dst = dst;
  514. }
  515. if (unlikely(!uh->check))
  516. uh->check = CSUM_MANGLED_0;
  517. } else {
  518. uh->source = src;
  519. uh->dest = dst;
  520. flow_key->tp.src = src;
  521. flow_key->tp.dst = dst;
  522. }
  523. skb_clear_hash(skb);
  524. return 0;
  525. }
  526. static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  527. const struct ovs_key_tcp *key,
  528. const struct ovs_key_tcp *mask)
  529. {
  530. struct tcphdr *th;
  531. __be16 src, dst;
  532. int err;
  533. err = skb_ensure_writable(skb, skb_transport_offset(skb) +
  534. sizeof(struct tcphdr));
  535. if (unlikely(err))
  536. return err;
  537. th = tcp_hdr(skb);
  538. src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
  539. if (likely(src != th->source)) {
  540. set_tp_port(skb, &th->source, src, &th->check);
  541. flow_key->tp.src = src;
  542. }
  543. dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
  544. if (likely(dst != th->dest)) {
  545. set_tp_port(skb, &th->dest, dst, &th->check);
  546. flow_key->tp.dst = dst;
  547. }
  548. skb_clear_hash(skb);
  549. return 0;
  550. }
  551. static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
  552. const struct ovs_key_sctp *key,
  553. const struct ovs_key_sctp *mask)
  554. {
  555. unsigned int sctphoff = skb_transport_offset(skb);
  556. struct sctphdr *sh;
  557. __le32 old_correct_csum, new_csum, old_csum;
  558. int err;
  559. err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
  560. if (unlikely(err))
  561. return err;
  562. sh = sctp_hdr(skb);
  563. old_csum = sh->checksum;
  564. old_correct_csum = sctp_compute_cksum(skb, sctphoff);
  565. sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
  566. sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
  567. new_csum = sctp_compute_cksum(skb, sctphoff);
  568. /* Carry any checksum errors through. */
  569. sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
  570. skb_clear_hash(skb);
  571. flow_key->tp.src = sh->source;
  572. flow_key->tp.dst = sh->dest;
  573. return 0;
  574. }
  575. static int ovs_vport_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  576. {
  577. struct ovs_frag_data *data = this_cpu_ptr(&ovs_frag_data_storage);
  578. struct vport *vport = data->vport;
  579. if (skb_cow_head(skb, data->l2_len) < 0) {
  580. kfree_skb(skb);
  581. return -ENOMEM;
  582. }
  583. __skb_dst_copy(skb, data->dst);
  584. *OVS_CB(skb) = data->cb;
  585. skb->inner_protocol = data->inner_protocol;
  586. skb->vlan_tci = data->vlan_tci;
  587. skb->vlan_proto = data->vlan_proto;
  588. /* Reconstruct the MAC header. */
  589. skb_push(skb, data->l2_len);
  590. memcpy(skb->data, &data->l2_data, data->l2_len);
  591. skb_postpush_rcsum(skb, skb->data, data->l2_len);
  592. skb_reset_mac_header(skb);
  593. if (eth_p_mpls(skb->protocol)) {
  594. skb->inner_network_header = skb->network_header;
  595. skb_set_network_header(skb, data->network_offset);
  596. skb_reset_mac_len(skb);
  597. }
  598. ovs_vport_send(vport, skb, data->mac_proto);
  599. return 0;
  600. }
  601. static unsigned int
  602. ovs_dst_get_mtu(const struct dst_entry *dst)
  603. {
  604. return dst->dev->mtu;
  605. }
  606. static struct dst_ops ovs_dst_ops = {
  607. .family = AF_UNSPEC,
  608. .mtu = ovs_dst_get_mtu,
  609. };
  610. /* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
  611. * ovs_vport_output(), which is called once per fragmented packet.
  612. */
  613. static void prepare_frag(struct vport *vport, struct sk_buff *skb,
  614. u16 orig_network_offset, u8 mac_proto)
  615. {
  616. unsigned int hlen = skb_network_offset(skb);
  617. struct ovs_frag_data *data;
  618. data = this_cpu_ptr(&ovs_frag_data_storage);
  619. data->dst = skb->_skb_refdst;
  620. data->vport = vport;
  621. data->cb = *OVS_CB(skb);
  622. data->inner_protocol = skb->inner_protocol;
  623. data->network_offset = orig_network_offset;
  624. data->vlan_tci = skb->vlan_tci;
  625. data->vlan_proto = skb->vlan_proto;
  626. data->mac_proto = mac_proto;
  627. data->l2_len = hlen;
  628. memcpy(&data->l2_data, skb->data, hlen);
  629. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  630. skb_pull(skb, hlen);
  631. }
  632. static void ovs_fragment(struct net *net, struct vport *vport,
  633. struct sk_buff *skb, u16 mru,
  634. struct sw_flow_key *key)
  635. {
  636. u16 orig_network_offset = 0;
  637. if (eth_p_mpls(skb->protocol)) {
  638. orig_network_offset = skb_network_offset(skb);
  639. skb->network_header = skb->inner_network_header;
  640. }
  641. if (skb_network_offset(skb) > MAX_L2_LEN) {
  642. OVS_NLERR(1, "L2 header too long to fragment");
  643. goto err;
  644. }
  645. if (key->eth.type == htons(ETH_P_IP)) {
  646. struct dst_entry ovs_dst;
  647. unsigned long orig_dst;
  648. prepare_frag(vport, skb, orig_network_offset,
  649. ovs_key_mac_proto(key));
  650. dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
  651. DST_OBSOLETE_NONE, DST_NOCOUNT);
  652. ovs_dst.dev = vport->dev;
  653. orig_dst = skb->_skb_refdst;
  654. skb_dst_set_noref(skb, &ovs_dst);
  655. IPCB(skb)->frag_max_size = mru;
  656. ip_do_fragment(net, skb->sk, skb, ovs_vport_output);
  657. refdst_drop(orig_dst);
  658. } else if (key->eth.type == htons(ETH_P_IPV6)) {
  659. const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
  660. unsigned long orig_dst;
  661. struct rt6_info ovs_rt;
  662. if (!v6ops) {
  663. goto err;
  664. }
  665. prepare_frag(vport, skb, orig_network_offset,
  666. ovs_key_mac_proto(key));
  667. memset(&ovs_rt, 0, sizeof(ovs_rt));
  668. dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
  669. DST_OBSOLETE_NONE, DST_NOCOUNT);
  670. ovs_rt.dst.dev = vport->dev;
  671. orig_dst = skb->_skb_refdst;
  672. skb_dst_set_noref(skb, &ovs_rt.dst);
  673. IP6CB(skb)->frag_max_size = mru;
  674. v6ops->fragment(net, skb->sk, skb, ovs_vport_output);
  675. refdst_drop(orig_dst);
  676. } else {
  677. WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
  678. ovs_vport_name(vport), ntohs(key->eth.type), mru,
  679. vport->dev->mtu);
  680. goto err;
  681. }
  682. return;
  683. err:
  684. kfree_skb(skb);
  685. }
  686. static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
  687. struct sw_flow_key *key)
  688. {
  689. struct vport *vport = ovs_vport_rcu(dp, out_port);
  690. if (likely(vport)) {
  691. u16 mru = OVS_CB(skb)->mru;
  692. u32 cutlen = OVS_CB(skb)->cutlen;
  693. if (unlikely(cutlen > 0)) {
  694. if (skb->len - cutlen > ovs_mac_header_len(key))
  695. pskb_trim(skb, skb->len - cutlen);
  696. else
  697. pskb_trim(skb, ovs_mac_header_len(key));
  698. }
  699. if (likely(!mru ||
  700. (skb->len <= mru + vport->dev->hard_header_len))) {
  701. ovs_vport_send(vport, skb, ovs_key_mac_proto(key));
  702. } else if (mru <= vport->dev->mtu) {
  703. struct net *net = read_pnet(&dp->net);
  704. ovs_fragment(net, vport, skb, mru, key);
  705. } else {
  706. kfree_skb(skb);
  707. }
  708. } else {
  709. kfree_skb(skb);
  710. }
  711. }
  712. static int output_userspace(struct datapath *dp, struct sk_buff *skb,
  713. struct sw_flow_key *key, const struct nlattr *attr,
  714. const struct nlattr *actions, int actions_len,
  715. uint32_t cutlen)
  716. {
  717. struct dp_upcall_info upcall;
  718. const struct nlattr *a;
  719. int rem;
  720. memset(&upcall, 0, sizeof(upcall));
  721. upcall.cmd = OVS_PACKET_CMD_ACTION;
  722. upcall.mru = OVS_CB(skb)->mru;
  723. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  724. a = nla_next(a, &rem)) {
  725. switch (nla_type(a)) {
  726. case OVS_USERSPACE_ATTR_USERDATA:
  727. upcall.userdata = a;
  728. break;
  729. case OVS_USERSPACE_ATTR_PID:
  730. upcall.portid = nla_get_u32(a);
  731. break;
  732. case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
  733. /* Get out tunnel info. */
  734. struct vport *vport;
  735. vport = ovs_vport_rcu(dp, nla_get_u32(a));
  736. if (vport) {
  737. int err;
  738. err = dev_fill_metadata_dst(vport->dev, skb);
  739. if (!err)
  740. upcall.egress_tun_info = skb_tunnel_info(skb);
  741. }
  742. break;
  743. }
  744. case OVS_USERSPACE_ATTR_ACTIONS: {
  745. /* Include actions. */
  746. upcall.actions = actions;
  747. upcall.actions_len = actions_len;
  748. break;
  749. }
  750. } /* End of switch. */
  751. }
  752. return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
  753. }
  754. static int sample(struct datapath *dp, struct sk_buff *skb,
  755. struct sw_flow_key *key, const struct nlattr *attr,
  756. const struct nlattr *actions, int actions_len)
  757. {
  758. const struct nlattr *acts_list = NULL;
  759. const struct nlattr *a;
  760. int rem;
  761. u32 cutlen = 0;
  762. for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
  763. a = nla_next(a, &rem)) {
  764. u32 probability;
  765. switch (nla_type(a)) {
  766. case OVS_SAMPLE_ATTR_PROBABILITY:
  767. probability = nla_get_u32(a);
  768. if (!probability || prandom_u32() > probability)
  769. return 0;
  770. break;
  771. case OVS_SAMPLE_ATTR_ACTIONS:
  772. acts_list = a;
  773. break;
  774. }
  775. }
  776. rem = nla_len(acts_list);
  777. a = nla_data(acts_list);
  778. /* Actions list is empty, do nothing */
  779. if (unlikely(!rem))
  780. return 0;
  781. /* The only known usage of sample action is having a single user-space
  782. * action, or having a truncate action followed by a single user-space
  783. * action. Treat this usage as a special case.
  784. * The output_userspace() should clone the skb to be sent to the
  785. * user space. This skb will be consumed by its caller.
  786. */
  787. if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
  788. struct ovs_action_trunc *trunc = nla_data(a);
  789. if (skb->len > trunc->max_len)
  790. cutlen = skb->len - trunc->max_len;
  791. a = nla_next(a, &rem);
  792. }
  793. if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
  794. nla_is_last(a, rem)))
  795. return output_userspace(dp, skb, key, a, actions,
  796. actions_len, cutlen);
  797. skb = skb_clone(skb, GFP_ATOMIC);
  798. if (!skb)
  799. /* Skip the sample action when out of memory. */
  800. return 0;
  801. if (!add_deferred_actions(skb, key, a)) {
  802. if (net_ratelimit())
  803. pr_warn("%s: deferred actions limit reached, dropping sample action\n",
  804. ovs_dp_name(dp));
  805. kfree_skb(skb);
  806. }
  807. return 0;
  808. }
  809. static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
  810. const struct nlattr *attr)
  811. {
  812. struct ovs_action_hash *hash_act = nla_data(attr);
  813. u32 hash = 0;
  814. /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
  815. hash = skb_get_hash(skb);
  816. hash = jhash_1word(hash, hash_act->hash_basis);
  817. if (!hash)
  818. hash = 0x1;
  819. key->ovs_flow_hash = hash;
  820. }
  821. static int execute_set_action(struct sk_buff *skb,
  822. struct sw_flow_key *flow_key,
  823. const struct nlattr *a)
  824. {
  825. /* Only tunnel set execution is supported without a mask. */
  826. if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
  827. struct ovs_tunnel_info *tun = nla_data(a);
  828. skb_dst_drop(skb);
  829. dst_hold((struct dst_entry *)tun->tun_dst);
  830. skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
  831. return 0;
  832. }
  833. return -EINVAL;
  834. }
  835. /* Mask is at the midpoint of the data. */
  836. #define get_mask(a, type) ((const type)nla_data(a) + 1)
  837. static int execute_masked_set_action(struct sk_buff *skb,
  838. struct sw_flow_key *flow_key,
  839. const struct nlattr *a)
  840. {
  841. int err = 0;
  842. switch (nla_type(a)) {
  843. case OVS_KEY_ATTR_PRIORITY:
  844. OVS_SET_MASKED(skb->priority, nla_get_u32(a),
  845. *get_mask(a, u32 *));
  846. flow_key->phy.priority = skb->priority;
  847. break;
  848. case OVS_KEY_ATTR_SKB_MARK:
  849. OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
  850. flow_key->phy.skb_mark = skb->mark;
  851. break;
  852. case OVS_KEY_ATTR_TUNNEL_INFO:
  853. /* Masked data not supported for tunnel. */
  854. err = -EINVAL;
  855. break;
  856. case OVS_KEY_ATTR_ETHERNET:
  857. err = set_eth_addr(skb, flow_key, nla_data(a),
  858. get_mask(a, struct ovs_key_ethernet *));
  859. break;
  860. case OVS_KEY_ATTR_IPV4:
  861. err = set_ipv4(skb, flow_key, nla_data(a),
  862. get_mask(a, struct ovs_key_ipv4 *));
  863. break;
  864. case OVS_KEY_ATTR_IPV6:
  865. err = set_ipv6(skb, flow_key, nla_data(a),
  866. get_mask(a, struct ovs_key_ipv6 *));
  867. break;
  868. case OVS_KEY_ATTR_TCP:
  869. err = set_tcp(skb, flow_key, nla_data(a),
  870. get_mask(a, struct ovs_key_tcp *));
  871. break;
  872. case OVS_KEY_ATTR_UDP:
  873. err = set_udp(skb, flow_key, nla_data(a),
  874. get_mask(a, struct ovs_key_udp *));
  875. break;
  876. case OVS_KEY_ATTR_SCTP:
  877. err = set_sctp(skb, flow_key, nla_data(a),
  878. get_mask(a, struct ovs_key_sctp *));
  879. break;
  880. case OVS_KEY_ATTR_MPLS:
  881. err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
  882. __be32 *));
  883. break;
  884. case OVS_KEY_ATTR_CT_STATE:
  885. case OVS_KEY_ATTR_CT_ZONE:
  886. case OVS_KEY_ATTR_CT_MARK:
  887. case OVS_KEY_ATTR_CT_LABELS:
  888. err = -EINVAL;
  889. break;
  890. }
  891. return err;
  892. }
  893. static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
  894. struct sw_flow_key *key,
  895. const struct nlattr *a, int rem)
  896. {
  897. struct deferred_action *da;
  898. int level;
  899. if (!is_flow_key_valid(key)) {
  900. int err;
  901. err = ovs_flow_key_update(skb, key);
  902. if (err)
  903. return err;
  904. }
  905. BUG_ON(!is_flow_key_valid(key));
  906. if (!nla_is_last(a, rem)) {
  907. /* Recirc action is the not the last action
  908. * of the action list, need to clone the skb.
  909. */
  910. skb = skb_clone(skb, GFP_ATOMIC);
  911. /* Skip the recirc action when out of memory, but
  912. * continue on with the rest of the action list.
  913. */
  914. if (!skb)
  915. return 0;
  916. }
  917. level = this_cpu_read(exec_actions_level);
  918. if (level <= OVS_DEFERRED_ACTION_THRESHOLD) {
  919. struct recirc_keys *rks = this_cpu_ptr(recirc_keys);
  920. struct sw_flow_key *recirc_key = &rks->key[level - 1];
  921. *recirc_key = *key;
  922. recirc_key->recirc_id = nla_get_u32(a);
  923. ovs_dp_process_packet(skb, recirc_key);
  924. return 0;
  925. }
  926. da = add_deferred_actions(skb, key, NULL);
  927. if (da) {
  928. da->pkt_key.recirc_id = nla_get_u32(a);
  929. } else {
  930. kfree_skb(skb);
  931. if (net_ratelimit())
  932. pr_warn("%s: deferred action limit reached, drop recirc action\n",
  933. ovs_dp_name(dp));
  934. }
  935. return 0;
  936. }
  937. /* Execute a list of actions against 'skb'. */
  938. static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
  939. struct sw_flow_key *key,
  940. const struct nlattr *attr, int len)
  941. {
  942. /* Every output action needs a separate clone of 'skb', but the common
  943. * case is just a single output action, so that doing a clone and
  944. * then freeing the original skbuff is wasteful. So the following code
  945. * is slightly obscure just to avoid that.
  946. */
  947. int prev_port = -1;
  948. const struct nlattr *a;
  949. int rem;
  950. for (a = attr, rem = len; rem > 0;
  951. a = nla_next(a, &rem)) {
  952. int err = 0;
  953. if (unlikely(prev_port != -1)) {
  954. struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
  955. if (out_skb)
  956. do_output(dp, out_skb, prev_port, key);
  957. OVS_CB(skb)->cutlen = 0;
  958. prev_port = -1;
  959. }
  960. switch (nla_type(a)) {
  961. case OVS_ACTION_ATTR_OUTPUT:
  962. prev_port = nla_get_u32(a);
  963. break;
  964. case OVS_ACTION_ATTR_TRUNC: {
  965. struct ovs_action_trunc *trunc = nla_data(a);
  966. if (skb->len > trunc->max_len)
  967. OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
  968. break;
  969. }
  970. case OVS_ACTION_ATTR_USERSPACE:
  971. output_userspace(dp, skb, key, a, attr,
  972. len, OVS_CB(skb)->cutlen);
  973. OVS_CB(skb)->cutlen = 0;
  974. break;
  975. case OVS_ACTION_ATTR_HASH:
  976. execute_hash(skb, key, a);
  977. break;
  978. case OVS_ACTION_ATTR_PUSH_MPLS:
  979. err = push_mpls(skb, key, nla_data(a));
  980. break;
  981. case OVS_ACTION_ATTR_POP_MPLS:
  982. err = pop_mpls(skb, key, nla_get_be16(a));
  983. break;
  984. case OVS_ACTION_ATTR_PUSH_VLAN:
  985. err = push_vlan(skb, key, nla_data(a));
  986. break;
  987. case OVS_ACTION_ATTR_POP_VLAN:
  988. err = pop_vlan(skb, key);
  989. break;
  990. case OVS_ACTION_ATTR_RECIRC:
  991. err = execute_recirc(dp, skb, key, a, rem);
  992. if (nla_is_last(a, rem)) {
  993. /* If this is the last action, the skb has
  994. * been consumed or freed.
  995. * Return immediately.
  996. */
  997. return err;
  998. }
  999. break;
  1000. case OVS_ACTION_ATTR_SET:
  1001. err = execute_set_action(skb, key, nla_data(a));
  1002. break;
  1003. case OVS_ACTION_ATTR_SET_MASKED:
  1004. case OVS_ACTION_ATTR_SET_TO_MASKED:
  1005. err = execute_masked_set_action(skb, key, nla_data(a));
  1006. break;
  1007. case OVS_ACTION_ATTR_SAMPLE:
  1008. err = sample(dp, skb, key, a, attr, len);
  1009. break;
  1010. case OVS_ACTION_ATTR_CT:
  1011. if (!is_flow_key_valid(key)) {
  1012. err = ovs_flow_key_update(skb, key);
  1013. if (err)
  1014. return err;
  1015. }
  1016. err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
  1017. nla_data(a));
  1018. /* Hide stolen IP fragments from user space. */
  1019. if (err)
  1020. return err == -EINPROGRESS ? 0 : err;
  1021. break;
  1022. case OVS_ACTION_ATTR_PUSH_ETH:
  1023. err = push_eth(skb, key, nla_data(a));
  1024. break;
  1025. case OVS_ACTION_ATTR_POP_ETH:
  1026. err = pop_eth(skb, key);
  1027. break;
  1028. }
  1029. if (unlikely(err)) {
  1030. kfree_skb(skb);
  1031. return err;
  1032. }
  1033. }
  1034. if (prev_port != -1)
  1035. do_output(dp, skb, prev_port, key);
  1036. else
  1037. consume_skb(skb);
  1038. return 0;
  1039. }
  1040. static void process_deferred_actions(struct datapath *dp)
  1041. {
  1042. struct action_fifo *fifo = this_cpu_ptr(action_fifos);
  1043. /* Do not touch the FIFO in case there is no deferred actions. */
  1044. if (action_fifo_is_empty(fifo))
  1045. return;
  1046. /* Finishing executing all deferred actions. */
  1047. do {
  1048. struct deferred_action *da = action_fifo_get(fifo);
  1049. struct sk_buff *skb = da->skb;
  1050. struct sw_flow_key *key = &da->pkt_key;
  1051. const struct nlattr *actions = da->actions;
  1052. if (actions)
  1053. do_execute_actions(dp, skb, key, actions,
  1054. nla_len(actions));
  1055. else
  1056. ovs_dp_process_packet(skb, key);
  1057. } while (!action_fifo_is_empty(fifo));
  1058. /* Reset FIFO for the next packet. */
  1059. action_fifo_init(fifo);
  1060. }
  1061. /* Execute a list of actions against 'skb'. */
  1062. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  1063. const struct sw_flow_actions *acts,
  1064. struct sw_flow_key *key)
  1065. {
  1066. int err, level;
  1067. level = __this_cpu_inc_return(exec_actions_level);
  1068. if (unlikely(level > OVS_RECURSION_LIMIT)) {
  1069. net_crit_ratelimited("ovs: recursion limit reached on datapath %s, probable configuration error\n",
  1070. ovs_dp_name(dp));
  1071. kfree_skb(skb);
  1072. err = -ENETDOWN;
  1073. goto out;
  1074. }
  1075. err = do_execute_actions(dp, skb, key,
  1076. acts->actions, acts->actions_len);
  1077. if (level == 1)
  1078. process_deferred_actions(dp);
  1079. out:
  1080. __this_cpu_dec(exec_actions_level);
  1081. return err;
  1082. }
  1083. int action_fifos_init(void)
  1084. {
  1085. action_fifos = alloc_percpu(struct action_fifo);
  1086. if (!action_fifos)
  1087. return -ENOMEM;
  1088. recirc_keys = alloc_percpu(struct recirc_keys);
  1089. if (!recirc_keys) {
  1090. free_percpu(action_fifos);
  1091. return -ENOMEM;
  1092. }
  1093. return 0;
  1094. }
  1095. void action_fifos_exit(void)
  1096. {
  1097. free_percpu(action_fifos);
  1098. free_percpu(recirc_keys);
  1099. }