actions.c 34 KB

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