datapath.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  1. /*
  2. * Copyright (c) 2007-2013 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/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/lockdep.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  58. static void rehash_flow_table(struct work_struct *work);
  59. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  60. int ovs_net_id __read_mostly;
  61. static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
  62. struct genl_multicast_group *grp)
  63. {
  64. genl_notify(skb, genl_info_net(info), info->snd_portid,
  65. grp->id, info->nlhdr, GFP_KERNEL);
  66. }
  67. /**
  68. * DOC: Locking:
  69. *
  70. * All writes e.g. Writes to device state (add/remove datapath, port, set
  71. * operations on vports, etc.), Writes to other state (flow table
  72. * modifications, set miscellaneous datapath parameters, etc.) are protected
  73. * by ovs_lock.
  74. *
  75. * Reads are protected by RCU.
  76. *
  77. * There are a few special cases (mostly stats) that have their own
  78. * synchronization but they nest under all of above and don't interact with
  79. * each other.
  80. *
  81. * The RTNL lock nests inside ovs_mutex.
  82. */
  83. static DEFINE_MUTEX(ovs_mutex);
  84. void ovs_lock(void)
  85. {
  86. mutex_lock(&ovs_mutex);
  87. }
  88. void ovs_unlock(void)
  89. {
  90. mutex_unlock(&ovs_mutex);
  91. }
  92. #ifdef CONFIG_LOCKDEP
  93. int lockdep_ovsl_is_held(void)
  94. {
  95. if (debug_locks)
  96. return lockdep_is_held(&ovs_mutex);
  97. else
  98. return 1;
  99. }
  100. #endif
  101. static struct vport *new_vport(const struct vport_parms *);
  102. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  103. const struct dp_upcall_info *);
  104. static int queue_userspace_packet(struct net *, int dp_ifindex,
  105. struct sk_buff *,
  106. const struct dp_upcall_info *);
  107. /* Must be called with rcu_read_lock or ovs_mutex. */
  108. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  109. {
  110. struct datapath *dp = NULL;
  111. struct net_device *dev;
  112. rcu_read_lock();
  113. dev = dev_get_by_index_rcu(net, dp_ifindex);
  114. if (dev) {
  115. struct vport *vport = ovs_internal_dev_get_vport(dev);
  116. if (vport)
  117. dp = vport->dp;
  118. }
  119. rcu_read_unlock();
  120. return dp;
  121. }
  122. /* Must be called with rcu_read_lock or ovs_mutex. */
  123. const char *ovs_dp_name(const struct datapath *dp)
  124. {
  125. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  126. return vport->ops->get_name(vport);
  127. }
  128. static int get_dpifindex(struct datapath *dp)
  129. {
  130. struct vport *local;
  131. int ifindex;
  132. rcu_read_lock();
  133. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  134. if (local)
  135. ifindex = netdev_vport_priv(local)->dev->ifindex;
  136. else
  137. ifindex = 0;
  138. rcu_read_unlock();
  139. return ifindex;
  140. }
  141. static void destroy_dp_rcu(struct rcu_head *rcu)
  142. {
  143. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  144. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
  145. free_percpu(dp->stats_percpu);
  146. release_net(ovs_dp_get_net(dp));
  147. kfree(dp->ports);
  148. kfree(dp);
  149. }
  150. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  151. u16 port_no)
  152. {
  153. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  154. }
  155. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  156. {
  157. struct vport *vport;
  158. struct hlist_head *head;
  159. head = vport_hash_bucket(dp, port_no);
  160. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  161. if (vport->port_no == port_no)
  162. return vport;
  163. }
  164. return NULL;
  165. }
  166. /* Called with ovs_mutex. */
  167. static struct vport *new_vport(const struct vport_parms *parms)
  168. {
  169. struct vport *vport;
  170. vport = ovs_vport_add(parms);
  171. if (!IS_ERR(vport)) {
  172. struct datapath *dp = parms->dp;
  173. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  174. hlist_add_head_rcu(&vport->dp_hash_node, head);
  175. }
  176. return vport;
  177. }
  178. void ovs_dp_detach_port(struct vport *p)
  179. {
  180. ASSERT_OVSL();
  181. /* First drop references to device. */
  182. hlist_del_rcu(&p->dp_hash_node);
  183. /* Then destroy it. */
  184. ovs_vport_del(p);
  185. }
  186. /* Must be called with rcu_read_lock. */
  187. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  188. {
  189. struct datapath *dp = p->dp;
  190. struct sw_flow *flow;
  191. struct dp_stats_percpu *stats;
  192. struct sw_flow_key key;
  193. u64 *stats_counter;
  194. int error;
  195. stats = this_cpu_ptr(dp->stats_percpu);
  196. /* Extract flow from 'skb' into 'key'. */
  197. error = ovs_flow_extract(skb, p->port_no, &key);
  198. if (unlikely(error)) {
  199. kfree_skb(skb);
  200. return;
  201. }
  202. /* Look up flow. */
  203. flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
  204. if (unlikely(!flow)) {
  205. struct dp_upcall_info upcall;
  206. upcall.cmd = OVS_PACKET_CMD_MISS;
  207. upcall.key = &key;
  208. upcall.userdata = NULL;
  209. upcall.portid = p->upcall_portid;
  210. ovs_dp_upcall(dp, skb, &upcall);
  211. consume_skb(skb);
  212. stats_counter = &stats->n_missed;
  213. goto out;
  214. }
  215. OVS_CB(skb)->flow = flow;
  216. OVS_CB(skb)->pkt_key = &key;
  217. stats_counter = &stats->n_hit;
  218. ovs_flow_used(OVS_CB(skb)->flow, skb);
  219. ovs_execute_actions(dp, skb);
  220. out:
  221. /* Update datapath statistics. */
  222. u64_stats_update_begin(&stats->sync);
  223. (*stats_counter)++;
  224. u64_stats_update_end(&stats->sync);
  225. }
  226. static struct genl_family dp_packet_genl_family = {
  227. .id = GENL_ID_GENERATE,
  228. .hdrsize = sizeof(struct ovs_header),
  229. .name = OVS_PACKET_FAMILY,
  230. .version = OVS_PACKET_VERSION,
  231. .maxattr = OVS_PACKET_ATTR_MAX,
  232. .netnsok = true,
  233. .parallel_ops = true,
  234. };
  235. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  236. const struct dp_upcall_info *upcall_info)
  237. {
  238. struct dp_stats_percpu *stats;
  239. int dp_ifindex;
  240. int err;
  241. if (upcall_info->portid == 0) {
  242. err = -ENOTCONN;
  243. goto err;
  244. }
  245. dp_ifindex = get_dpifindex(dp);
  246. if (!dp_ifindex) {
  247. err = -ENODEV;
  248. goto err;
  249. }
  250. if (!skb_is_gso(skb))
  251. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  252. else
  253. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  254. if (err)
  255. goto err;
  256. return 0;
  257. err:
  258. stats = this_cpu_ptr(dp->stats_percpu);
  259. u64_stats_update_begin(&stats->sync);
  260. stats->n_lost++;
  261. u64_stats_update_end(&stats->sync);
  262. return err;
  263. }
  264. static int queue_gso_packets(struct net *net, int dp_ifindex,
  265. struct sk_buff *skb,
  266. const struct dp_upcall_info *upcall_info)
  267. {
  268. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  269. struct dp_upcall_info later_info;
  270. struct sw_flow_key later_key;
  271. struct sk_buff *segs, *nskb;
  272. int err;
  273. segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
  274. if (IS_ERR(segs))
  275. return PTR_ERR(segs);
  276. /* Queue all of the segments. */
  277. skb = segs;
  278. do {
  279. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  280. if (err)
  281. break;
  282. if (skb == segs && gso_type & SKB_GSO_UDP) {
  283. /* The initial flow key extracted by ovs_flow_extract()
  284. * in this case is for a first fragment, so we need to
  285. * properly mark later fragments.
  286. */
  287. later_key = *upcall_info->key;
  288. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  289. later_info = *upcall_info;
  290. later_info.key = &later_key;
  291. upcall_info = &later_info;
  292. }
  293. } while ((skb = skb->next));
  294. /* Free all of the segments. */
  295. skb = segs;
  296. do {
  297. nskb = skb->next;
  298. if (err)
  299. kfree_skb(skb);
  300. else
  301. consume_skb(skb);
  302. } while ((skb = nskb));
  303. return err;
  304. }
  305. static size_t key_attr_size(void)
  306. {
  307. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  308. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  309. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  310. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  311. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  312. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  313. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  314. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  315. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  316. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  317. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  318. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  319. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  320. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  321. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  322. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  323. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  324. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  325. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  326. }
  327. static size_t upcall_msg_size(const struct sk_buff *skb,
  328. const struct nlattr *userdata)
  329. {
  330. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  331. + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
  332. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  333. /* OVS_PACKET_ATTR_USERDATA */
  334. if (userdata)
  335. size += NLA_ALIGN(userdata->nla_len);
  336. return size;
  337. }
  338. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  339. struct sk_buff *skb,
  340. const struct dp_upcall_info *upcall_info)
  341. {
  342. struct ovs_header *upcall;
  343. struct sk_buff *nskb = NULL;
  344. struct sk_buff *user_skb; /* to be queued to userspace */
  345. struct nlattr *nla;
  346. int err;
  347. if (vlan_tx_tag_present(skb)) {
  348. nskb = skb_clone(skb, GFP_ATOMIC);
  349. if (!nskb)
  350. return -ENOMEM;
  351. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  352. if (!nskb)
  353. return -ENOMEM;
  354. nskb->vlan_tci = 0;
  355. skb = nskb;
  356. }
  357. if (nla_attr_size(skb->len) > USHRT_MAX) {
  358. err = -EFBIG;
  359. goto out;
  360. }
  361. user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
  362. if (!user_skb) {
  363. err = -ENOMEM;
  364. goto out;
  365. }
  366. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  367. 0, upcall_info->cmd);
  368. upcall->dp_ifindex = dp_ifindex;
  369. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  370. ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
  371. nla_nest_end(user_skb, nla);
  372. if (upcall_info->userdata)
  373. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  374. nla_len(upcall_info->userdata),
  375. nla_data(upcall_info->userdata));
  376. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  377. skb_copy_and_csum_dev(skb, nla_data(nla));
  378. genlmsg_end(user_skb, upcall);
  379. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  380. out:
  381. kfree_skb(nskb);
  382. return err;
  383. }
  384. /* Called with ovs_mutex. */
  385. static int flush_flows(struct datapath *dp)
  386. {
  387. struct flow_table *old_table;
  388. struct flow_table *new_table;
  389. old_table = ovsl_dereference(dp->table);
  390. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  391. if (!new_table)
  392. return -ENOMEM;
  393. rcu_assign_pointer(dp->table, new_table);
  394. ovs_flow_tbl_destroy(old_table, true);
  395. return 0;
  396. }
  397. static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
  398. {
  399. struct sw_flow_actions *acts;
  400. int new_acts_size;
  401. int req_size = NLA_ALIGN(attr_len);
  402. int next_offset = offsetof(struct sw_flow_actions, actions) +
  403. (*sfa)->actions_len;
  404. if (req_size <= (ksize(*sfa) - next_offset))
  405. goto out;
  406. new_acts_size = ksize(*sfa) * 2;
  407. if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
  408. if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
  409. return ERR_PTR(-EMSGSIZE);
  410. new_acts_size = MAX_ACTIONS_BUFSIZE;
  411. }
  412. acts = ovs_flow_actions_alloc(new_acts_size);
  413. if (IS_ERR(acts))
  414. return (void *)acts;
  415. memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
  416. acts->actions_len = (*sfa)->actions_len;
  417. kfree(*sfa);
  418. *sfa = acts;
  419. out:
  420. (*sfa)->actions_len += req_size;
  421. return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
  422. }
  423. static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
  424. {
  425. struct nlattr *a;
  426. a = reserve_sfa_size(sfa, nla_attr_size(len));
  427. if (IS_ERR(a))
  428. return PTR_ERR(a);
  429. a->nla_type = attrtype;
  430. a->nla_len = nla_attr_size(len);
  431. if (data)
  432. memcpy(nla_data(a), data, len);
  433. memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
  434. return 0;
  435. }
  436. static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
  437. {
  438. int used = (*sfa)->actions_len;
  439. int err;
  440. err = add_action(sfa, attrtype, NULL, 0);
  441. if (err)
  442. return err;
  443. return used;
  444. }
  445. static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
  446. {
  447. struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
  448. a->nla_len = sfa->actions_len - st_offset;
  449. }
  450. static int validate_and_copy_actions(const struct nlattr *attr,
  451. const struct sw_flow_key *key, int depth,
  452. struct sw_flow_actions **sfa);
  453. static int validate_and_copy_sample(const struct nlattr *attr,
  454. const struct sw_flow_key *key, int depth,
  455. struct sw_flow_actions **sfa)
  456. {
  457. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  458. const struct nlattr *probability, *actions;
  459. const struct nlattr *a;
  460. int rem, start, err, st_acts;
  461. memset(attrs, 0, sizeof(attrs));
  462. nla_for_each_nested(a, attr, rem) {
  463. int type = nla_type(a);
  464. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  465. return -EINVAL;
  466. attrs[type] = a;
  467. }
  468. if (rem)
  469. return -EINVAL;
  470. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  471. if (!probability || nla_len(probability) != sizeof(u32))
  472. return -EINVAL;
  473. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  474. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  475. return -EINVAL;
  476. /* validation done, copy sample action. */
  477. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
  478. if (start < 0)
  479. return start;
  480. err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
  481. if (err)
  482. return err;
  483. st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
  484. if (st_acts < 0)
  485. return st_acts;
  486. err = validate_and_copy_actions(actions, key, depth + 1, sfa);
  487. if (err)
  488. return err;
  489. add_nested_action_end(*sfa, st_acts);
  490. add_nested_action_end(*sfa, start);
  491. return 0;
  492. }
  493. static int validate_tp_port(const struct sw_flow_key *flow_key)
  494. {
  495. if (flow_key->eth.type == htons(ETH_P_IP)) {
  496. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  497. return 0;
  498. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  499. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  500. return 0;
  501. }
  502. return -EINVAL;
  503. }
  504. static int validate_and_copy_set_tun(const struct nlattr *attr,
  505. struct sw_flow_actions **sfa)
  506. {
  507. struct sw_flow_match match;
  508. struct sw_flow_key key;
  509. int err, start;
  510. ovs_match_init(&match, &key, NULL);
  511. err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
  512. if (err)
  513. return err;
  514. start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
  515. if (start < 0)
  516. return start;
  517. err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
  518. sizeof(match.key->tun_key));
  519. add_nested_action_end(*sfa, start);
  520. return err;
  521. }
  522. static int validate_set(const struct nlattr *a,
  523. const struct sw_flow_key *flow_key,
  524. struct sw_flow_actions **sfa,
  525. bool *set_tun)
  526. {
  527. const struct nlattr *ovs_key = nla_data(a);
  528. int key_type = nla_type(ovs_key);
  529. /* There can be only one key in a action */
  530. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  531. return -EINVAL;
  532. if (key_type > OVS_KEY_ATTR_MAX ||
  533. (ovs_key_lens[key_type] != nla_len(ovs_key) &&
  534. ovs_key_lens[key_type] != -1))
  535. return -EINVAL;
  536. switch (key_type) {
  537. const struct ovs_key_ipv4 *ipv4_key;
  538. const struct ovs_key_ipv6 *ipv6_key;
  539. int err;
  540. case OVS_KEY_ATTR_PRIORITY:
  541. case OVS_KEY_ATTR_SKB_MARK:
  542. case OVS_KEY_ATTR_ETHERNET:
  543. break;
  544. case OVS_KEY_ATTR_TUNNEL:
  545. *set_tun = true;
  546. err = validate_and_copy_set_tun(a, sfa);
  547. if (err)
  548. return err;
  549. break;
  550. case OVS_KEY_ATTR_IPV4:
  551. if (flow_key->eth.type != htons(ETH_P_IP))
  552. return -EINVAL;
  553. if (!flow_key->ip.proto)
  554. return -EINVAL;
  555. ipv4_key = nla_data(ovs_key);
  556. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  557. return -EINVAL;
  558. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  559. return -EINVAL;
  560. break;
  561. case OVS_KEY_ATTR_IPV6:
  562. if (flow_key->eth.type != htons(ETH_P_IPV6))
  563. return -EINVAL;
  564. if (!flow_key->ip.proto)
  565. return -EINVAL;
  566. ipv6_key = nla_data(ovs_key);
  567. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  568. return -EINVAL;
  569. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  570. return -EINVAL;
  571. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  572. return -EINVAL;
  573. break;
  574. case OVS_KEY_ATTR_TCP:
  575. if (flow_key->ip.proto != IPPROTO_TCP)
  576. return -EINVAL;
  577. return validate_tp_port(flow_key);
  578. case OVS_KEY_ATTR_UDP:
  579. if (flow_key->ip.proto != IPPROTO_UDP)
  580. return -EINVAL;
  581. return validate_tp_port(flow_key);
  582. default:
  583. return -EINVAL;
  584. }
  585. return 0;
  586. }
  587. static int validate_userspace(const struct nlattr *attr)
  588. {
  589. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  590. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  591. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  592. };
  593. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  594. int error;
  595. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  596. attr, userspace_policy);
  597. if (error)
  598. return error;
  599. if (!a[OVS_USERSPACE_ATTR_PID] ||
  600. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  601. return -EINVAL;
  602. return 0;
  603. }
  604. static int copy_action(const struct nlattr *from,
  605. struct sw_flow_actions **sfa)
  606. {
  607. int totlen = NLA_ALIGN(from->nla_len);
  608. struct nlattr *to;
  609. to = reserve_sfa_size(sfa, from->nla_len);
  610. if (IS_ERR(to))
  611. return PTR_ERR(to);
  612. memcpy(to, from, totlen);
  613. return 0;
  614. }
  615. static int validate_and_copy_actions(const struct nlattr *attr,
  616. const struct sw_flow_key *key,
  617. int depth,
  618. struct sw_flow_actions **sfa)
  619. {
  620. const struct nlattr *a;
  621. int rem, err;
  622. if (depth >= SAMPLE_ACTION_DEPTH)
  623. return -EOVERFLOW;
  624. nla_for_each_nested(a, attr, rem) {
  625. /* Expected argument lengths, (u32)-1 for variable length. */
  626. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  627. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  628. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  629. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  630. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  631. [OVS_ACTION_ATTR_SET] = (u32)-1,
  632. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  633. };
  634. const struct ovs_action_push_vlan *vlan;
  635. int type = nla_type(a);
  636. bool skip_copy;
  637. if (type > OVS_ACTION_ATTR_MAX ||
  638. (action_lens[type] != nla_len(a) &&
  639. action_lens[type] != (u32)-1))
  640. return -EINVAL;
  641. skip_copy = false;
  642. switch (type) {
  643. case OVS_ACTION_ATTR_UNSPEC:
  644. return -EINVAL;
  645. case OVS_ACTION_ATTR_USERSPACE:
  646. err = validate_userspace(a);
  647. if (err)
  648. return err;
  649. break;
  650. case OVS_ACTION_ATTR_OUTPUT:
  651. if (nla_get_u32(a) >= DP_MAX_PORTS)
  652. return -EINVAL;
  653. break;
  654. case OVS_ACTION_ATTR_POP_VLAN:
  655. break;
  656. case OVS_ACTION_ATTR_PUSH_VLAN:
  657. vlan = nla_data(a);
  658. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  659. return -EINVAL;
  660. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  661. return -EINVAL;
  662. break;
  663. case OVS_ACTION_ATTR_SET:
  664. err = validate_set(a, key, sfa, &skip_copy);
  665. if (err)
  666. return err;
  667. break;
  668. case OVS_ACTION_ATTR_SAMPLE:
  669. err = validate_and_copy_sample(a, key, depth, sfa);
  670. if (err)
  671. return err;
  672. skip_copy = true;
  673. break;
  674. default:
  675. return -EINVAL;
  676. }
  677. if (!skip_copy) {
  678. err = copy_action(a, sfa);
  679. if (err)
  680. return err;
  681. }
  682. }
  683. if (rem > 0)
  684. return -EINVAL;
  685. return 0;
  686. }
  687. static void clear_stats(struct sw_flow *flow)
  688. {
  689. flow->used = 0;
  690. flow->tcp_flags = 0;
  691. flow->packet_count = 0;
  692. flow->byte_count = 0;
  693. }
  694. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  695. {
  696. struct ovs_header *ovs_header = info->userhdr;
  697. struct nlattr **a = info->attrs;
  698. struct sw_flow_actions *acts;
  699. struct sk_buff *packet;
  700. struct sw_flow *flow;
  701. struct datapath *dp;
  702. struct ethhdr *eth;
  703. int len;
  704. int err;
  705. err = -EINVAL;
  706. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  707. !a[OVS_PACKET_ATTR_ACTIONS])
  708. goto err;
  709. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  710. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  711. err = -ENOMEM;
  712. if (!packet)
  713. goto err;
  714. skb_reserve(packet, NET_IP_ALIGN);
  715. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  716. skb_reset_mac_header(packet);
  717. eth = eth_hdr(packet);
  718. /* Normally, setting the skb 'protocol' field would be handled by a
  719. * call to eth_type_trans(), but it assumes there's a sending
  720. * device, which we may not have. */
  721. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  722. packet->protocol = eth->h_proto;
  723. else
  724. packet->protocol = htons(ETH_P_802_2);
  725. /* Build an sw_flow for sending this packet. */
  726. flow = ovs_flow_alloc();
  727. err = PTR_ERR(flow);
  728. if (IS_ERR(flow))
  729. goto err_kfree_skb;
  730. err = ovs_flow_extract(packet, -1, &flow->key);
  731. if (err)
  732. goto err_flow_free;
  733. err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
  734. if (err)
  735. goto err_flow_free;
  736. acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  737. err = PTR_ERR(acts);
  738. if (IS_ERR(acts))
  739. goto err_flow_free;
  740. err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
  741. rcu_assign_pointer(flow->sf_acts, acts);
  742. if (err)
  743. goto err_flow_free;
  744. OVS_CB(packet)->flow = flow;
  745. OVS_CB(packet)->pkt_key = &flow->key;
  746. packet->priority = flow->key.phy.priority;
  747. packet->mark = flow->key.phy.skb_mark;
  748. rcu_read_lock();
  749. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  750. err = -ENODEV;
  751. if (!dp)
  752. goto err_unlock;
  753. local_bh_disable();
  754. err = ovs_execute_actions(dp, packet);
  755. local_bh_enable();
  756. rcu_read_unlock();
  757. ovs_flow_free(flow, false);
  758. return err;
  759. err_unlock:
  760. rcu_read_unlock();
  761. err_flow_free:
  762. ovs_flow_free(flow, false);
  763. err_kfree_skb:
  764. kfree_skb(packet);
  765. err:
  766. return err;
  767. }
  768. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  769. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  770. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  771. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  772. };
  773. static struct genl_ops dp_packet_genl_ops[] = {
  774. { .cmd = OVS_PACKET_CMD_EXECUTE,
  775. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  776. .policy = packet_policy,
  777. .doit = ovs_packet_cmd_execute
  778. }
  779. };
  780. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  781. {
  782. struct flow_table *table;
  783. int i;
  784. table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
  785. stats->n_flows = ovs_flow_tbl_count(table);
  786. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  787. for_each_possible_cpu(i) {
  788. const struct dp_stats_percpu *percpu_stats;
  789. struct dp_stats_percpu local_stats;
  790. unsigned int start;
  791. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  792. do {
  793. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  794. local_stats = *percpu_stats;
  795. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  796. stats->n_hit += local_stats.n_hit;
  797. stats->n_missed += local_stats.n_missed;
  798. stats->n_lost += local_stats.n_lost;
  799. }
  800. }
  801. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  802. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  803. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  804. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  805. };
  806. static struct genl_family dp_flow_genl_family = {
  807. .id = GENL_ID_GENERATE,
  808. .hdrsize = sizeof(struct ovs_header),
  809. .name = OVS_FLOW_FAMILY,
  810. .version = OVS_FLOW_VERSION,
  811. .maxattr = OVS_FLOW_ATTR_MAX,
  812. .netnsok = true,
  813. .parallel_ops = true,
  814. };
  815. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  816. .name = OVS_FLOW_MCGROUP
  817. };
  818. static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
  819. static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
  820. {
  821. const struct nlattr *a;
  822. struct nlattr *start;
  823. int err = 0, rem;
  824. start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
  825. if (!start)
  826. return -EMSGSIZE;
  827. nla_for_each_nested(a, attr, rem) {
  828. int type = nla_type(a);
  829. struct nlattr *st_sample;
  830. switch (type) {
  831. case OVS_SAMPLE_ATTR_PROBABILITY:
  832. if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
  833. return -EMSGSIZE;
  834. break;
  835. case OVS_SAMPLE_ATTR_ACTIONS:
  836. st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
  837. if (!st_sample)
  838. return -EMSGSIZE;
  839. err = actions_to_attr(nla_data(a), nla_len(a), skb);
  840. if (err)
  841. return err;
  842. nla_nest_end(skb, st_sample);
  843. break;
  844. }
  845. }
  846. nla_nest_end(skb, start);
  847. return err;
  848. }
  849. static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
  850. {
  851. const struct nlattr *ovs_key = nla_data(a);
  852. int key_type = nla_type(ovs_key);
  853. struct nlattr *start;
  854. int err;
  855. switch (key_type) {
  856. case OVS_KEY_ATTR_IPV4_TUNNEL:
  857. start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
  858. if (!start)
  859. return -EMSGSIZE;
  860. err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
  861. nla_data(ovs_key));
  862. if (err)
  863. return err;
  864. nla_nest_end(skb, start);
  865. break;
  866. default:
  867. if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
  868. return -EMSGSIZE;
  869. break;
  870. }
  871. return 0;
  872. }
  873. static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
  874. {
  875. const struct nlattr *a;
  876. int rem, err;
  877. nla_for_each_attr(a, attr, len, rem) {
  878. int type = nla_type(a);
  879. switch (type) {
  880. case OVS_ACTION_ATTR_SET:
  881. err = set_action_to_attr(a, skb);
  882. if (err)
  883. return err;
  884. break;
  885. case OVS_ACTION_ATTR_SAMPLE:
  886. err = sample_action_to_attr(a, skb);
  887. if (err)
  888. return err;
  889. break;
  890. default:
  891. if (nla_put(skb, type, nla_len(a), nla_data(a)))
  892. return -EMSGSIZE;
  893. break;
  894. }
  895. }
  896. return 0;
  897. }
  898. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  899. {
  900. return NLMSG_ALIGN(sizeof(struct ovs_header))
  901. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  902. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  903. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  904. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  905. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  906. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  907. }
  908. /* Called with ovs_mutex. */
  909. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  910. struct sk_buff *skb, u32 portid,
  911. u32 seq, u32 flags, u8 cmd)
  912. {
  913. const int skb_orig_len = skb->len;
  914. struct nlattr *start;
  915. struct ovs_flow_stats stats;
  916. struct ovs_header *ovs_header;
  917. struct nlattr *nla;
  918. unsigned long used;
  919. u8 tcp_flags;
  920. int err;
  921. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  922. if (!ovs_header)
  923. return -EMSGSIZE;
  924. ovs_header->dp_ifindex = get_dpifindex(dp);
  925. /* Fill flow key. */
  926. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  927. if (!nla)
  928. goto nla_put_failure;
  929. err = ovs_flow_to_nlattrs(&flow->unmasked_key,
  930. &flow->unmasked_key, skb);
  931. if (err)
  932. goto error;
  933. nla_nest_end(skb, nla);
  934. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  935. if (!nla)
  936. goto nla_put_failure;
  937. err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
  938. if (err)
  939. goto error;
  940. nla_nest_end(skb, nla);
  941. spin_lock_bh(&flow->lock);
  942. used = flow->used;
  943. stats.n_packets = flow->packet_count;
  944. stats.n_bytes = flow->byte_count;
  945. tcp_flags = flow->tcp_flags;
  946. spin_unlock_bh(&flow->lock);
  947. if (used &&
  948. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  949. goto nla_put_failure;
  950. if (stats.n_packets &&
  951. nla_put(skb, OVS_FLOW_ATTR_STATS,
  952. sizeof(struct ovs_flow_stats), &stats))
  953. goto nla_put_failure;
  954. if (tcp_flags &&
  955. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  956. goto nla_put_failure;
  957. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  958. * this is the first flow to be dumped into 'skb'. This is unusual for
  959. * Netlink but individual action lists can be longer than
  960. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  961. * The userspace caller can always fetch the actions separately if it
  962. * really wants them. (Most userspace callers in fact don't care.)
  963. *
  964. * This can only fail for dump operations because the skb is always
  965. * properly sized for single flows.
  966. */
  967. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  968. if (start) {
  969. const struct sw_flow_actions *sf_acts;
  970. sf_acts = rcu_dereference_check(flow->sf_acts,
  971. lockdep_ovsl_is_held());
  972. err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
  973. if (!err)
  974. nla_nest_end(skb, start);
  975. else {
  976. if (skb_orig_len)
  977. goto error;
  978. nla_nest_cancel(skb, start);
  979. }
  980. } else if (skb_orig_len)
  981. goto nla_put_failure;
  982. return genlmsg_end(skb, ovs_header);
  983. nla_put_failure:
  984. err = -EMSGSIZE;
  985. error:
  986. genlmsg_cancel(skb, ovs_header);
  987. return err;
  988. }
  989. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  990. {
  991. const struct sw_flow_actions *sf_acts;
  992. sf_acts = ovsl_dereference(flow->sf_acts);
  993. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  994. }
  995. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  996. struct datapath *dp,
  997. u32 portid, u32 seq, u8 cmd)
  998. {
  999. struct sk_buff *skb;
  1000. int retval;
  1001. skb = ovs_flow_cmd_alloc_info(flow);
  1002. if (!skb)
  1003. return ERR_PTR(-ENOMEM);
  1004. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  1005. BUG_ON(retval < 0);
  1006. return skb;
  1007. }
  1008. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  1009. {
  1010. struct nlattr **a = info->attrs;
  1011. struct ovs_header *ovs_header = info->userhdr;
  1012. struct sw_flow_key key, masked_key;
  1013. struct sw_flow *flow = NULL;
  1014. struct sw_flow_mask mask;
  1015. struct sk_buff *reply;
  1016. struct datapath *dp;
  1017. struct flow_table *table;
  1018. struct sw_flow_actions *acts = NULL;
  1019. struct sw_flow_match match;
  1020. int error;
  1021. /* Extract key. */
  1022. error = -EINVAL;
  1023. if (!a[OVS_FLOW_ATTR_KEY])
  1024. goto error;
  1025. ovs_match_init(&match, &key, &mask);
  1026. error = ovs_match_from_nlattrs(&match,
  1027. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  1028. if (error)
  1029. goto error;
  1030. /* Validate actions. */
  1031. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  1032. acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  1033. error = PTR_ERR(acts);
  1034. if (IS_ERR(acts))
  1035. goto error;
  1036. ovs_flow_key_mask(&masked_key, &key, &mask);
  1037. error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  1038. &masked_key, 0, &acts);
  1039. if (error) {
  1040. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  1041. goto err_kfree;
  1042. }
  1043. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  1044. error = -EINVAL;
  1045. goto error;
  1046. }
  1047. ovs_lock();
  1048. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1049. error = -ENODEV;
  1050. if (!dp)
  1051. goto err_unlock_ovs;
  1052. table = ovsl_dereference(dp->table);
  1053. /* Check if this is a duplicate flow */
  1054. flow = ovs_flow_lookup(table, &key);
  1055. if (!flow) {
  1056. struct sw_flow_mask *mask_p;
  1057. /* Bail out if we're not allowed to create a new flow. */
  1058. error = -ENOENT;
  1059. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  1060. goto err_unlock_ovs;
  1061. /* Expand table, if necessary, to make room. */
  1062. if (ovs_flow_tbl_need_to_expand(table)) {
  1063. struct flow_table *new_table;
  1064. new_table = ovs_flow_tbl_expand(table);
  1065. if (!IS_ERR(new_table)) {
  1066. rcu_assign_pointer(dp->table, new_table);
  1067. ovs_flow_tbl_destroy(table, true);
  1068. table = ovsl_dereference(dp->table);
  1069. }
  1070. }
  1071. /* Allocate flow. */
  1072. flow = ovs_flow_alloc();
  1073. if (IS_ERR(flow)) {
  1074. error = PTR_ERR(flow);
  1075. goto err_unlock_ovs;
  1076. }
  1077. clear_stats(flow);
  1078. flow->key = masked_key;
  1079. flow->unmasked_key = key;
  1080. /* Make sure mask is unique in the system */
  1081. mask_p = ovs_sw_flow_mask_find(table, &mask);
  1082. if (!mask_p) {
  1083. /* Allocate a new mask if none exsits. */
  1084. mask_p = ovs_sw_flow_mask_alloc();
  1085. if (!mask_p)
  1086. goto err_flow_free;
  1087. mask_p->key = mask.key;
  1088. mask_p->range = mask.range;
  1089. ovs_sw_flow_mask_insert(table, mask_p);
  1090. }
  1091. ovs_sw_flow_mask_add_ref(mask_p);
  1092. flow->mask = mask_p;
  1093. rcu_assign_pointer(flow->sf_acts, acts);
  1094. /* Put flow in bucket. */
  1095. ovs_flow_insert(table, flow);
  1096. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1097. info->snd_seq, OVS_FLOW_CMD_NEW);
  1098. } else {
  1099. /* We found a matching flow. */
  1100. struct sw_flow_actions *old_acts;
  1101. /* Bail out if we're not allowed to modify an existing flow.
  1102. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  1103. * because Generic Netlink treats the latter as a dump
  1104. * request. We also accept NLM_F_EXCL in case that bug ever
  1105. * gets fixed.
  1106. */
  1107. error = -EEXIST;
  1108. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  1109. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  1110. goto err_unlock_ovs;
  1111. /* The unmasked key has to be the same for flow updates. */
  1112. error = -EINVAL;
  1113. if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
  1114. OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
  1115. goto err_unlock_ovs;
  1116. }
  1117. /* Update actions. */
  1118. old_acts = ovsl_dereference(flow->sf_acts);
  1119. rcu_assign_pointer(flow->sf_acts, acts);
  1120. ovs_flow_deferred_free_acts(old_acts);
  1121. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1122. info->snd_seq, OVS_FLOW_CMD_NEW);
  1123. /* Clear stats. */
  1124. if (a[OVS_FLOW_ATTR_CLEAR]) {
  1125. spin_lock_bh(&flow->lock);
  1126. clear_stats(flow);
  1127. spin_unlock_bh(&flow->lock);
  1128. }
  1129. }
  1130. ovs_unlock();
  1131. if (!IS_ERR(reply))
  1132. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  1133. else
  1134. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1135. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  1136. return 0;
  1137. err_flow_free:
  1138. ovs_flow_free(flow, false);
  1139. err_unlock_ovs:
  1140. ovs_unlock();
  1141. err_kfree:
  1142. kfree(acts);
  1143. error:
  1144. return error;
  1145. }
  1146. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1147. {
  1148. struct nlattr **a = info->attrs;
  1149. struct ovs_header *ovs_header = info->userhdr;
  1150. struct sw_flow_key key;
  1151. struct sk_buff *reply;
  1152. struct sw_flow *flow;
  1153. struct datapath *dp;
  1154. struct flow_table *table;
  1155. struct sw_flow_match match;
  1156. int err;
  1157. if (!a[OVS_FLOW_ATTR_KEY]) {
  1158. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  1159. return -EINVAL;
  1160. }
  1161. ovs_match_init(&match, &key, NULL);
  1162. err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  1163. if (err)
  1164. return err;
  1165. ovs_lock();
  1166. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1167. if (!dp) {
  1168. err = -ENODEV;
  1169. goto unlock;
  1170. }
  1171. table = ovsl_dereference(dp->table);
  1172. flow = ovs_flow_lookup_unmasked_key(table, &match);
  1173. if (!flow) {
  1174. err = -ENOENT;
  1175. goto unlock;
  1176. }
  1177. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  1178. info->snd_seq, OVS_FLOW_CMD_NEW);
  1179. if (IS_ERR(reply)) {
  1180. err = PTR_ERR(reply);
  1181. goto unlock;
  1182. }
  1183. ovs_unlock();
  1184. return genlmsg_reply(reply, info);
  1185. unlock:
  1186. ovs_unlock();
  1187. return err;
  1188. }
  1189. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1190. {
  1191. struct nlattr **a = info->attrs;
  1192. struct ovs_header *ovs_header = info->userhdr;
  1193. struct sw_flow_key key;
  1194. struct sk_buff *reply;
  1195. struct sw_flow *flow;
  1196. struct datapath *dp;
  1197. struct flow_table *table;
  1198. struct sw_flow_match match;
  1199. int err;
  1200. ovs_lock();
  1201. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1202. if (!dp) {
  1203. err = -ENODEV;
  1204. goto unlock;
  1205. }
  1206. if (!a[OVS_FLOW_ATTR_KEY]) {
  1207. err = flush_flows(dp);
  1208. goto unlock;
  1209. }
  1210. ovs_match_init(&match, &key, NULL);
  1211. err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  1212. if (err)
  1213. goto unlock;
  1214. table = ovsl_dereference(dp->table);
  1215. flow = ovs_flow_lookup_unmasked_key(table, &match);
  1216. if (!flow) {
  1217. err = -ENOENT;
  1218. goto unlock;
  1219. }
  1220. reply = ovs_flow_cmd_alloc_info(flow);
  1221. if (!reply) {
  1222. err = -ENOMEM;
  1223. goto unlock;
  1224. }
  1225. ovs_flow_remove(table, flow);
  1226. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  1227. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  1228. BUG_ON(err < 0);
  1229. ovs_flow_free(flow, true);
  1230. ovs_unlock();
  1231. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  1232. return 0;
  1233. unlock:
  1234. ovs_unlock();
  1235. return err;
  1236. }
  1237. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1238. {
  1239. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1240. struct datapath *dp;
  1241. struct flow_table *table;
  1242. rcu_read_lock();
  1243. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1244. if (!dp) {
  1245. rcu_read_unlock();
  1246. return -ENODEV;
  1247. }
  1248. table = rcu_dereference(dp->table);
  1249. for (;;) {
  1250. struct sw_flow *flow;
  1251. u32 bucket, obj;
  1252. bucket = cb->args[0];
  1253. obj = cb->args[1];
  1254. flow = ovs_flow_dump_next(table, &bucket, &obj);
  1255. if (!flow)
  1256. break;
  1257. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  1258. NETLINK_CB(cb->skb).portid,
  1259. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1260. OVS_FLOW_CMD_NEW) < 0)
  1261. break;
  1262. cb->args[0] = bucket;
  1263. cb->args[1] = obj;
  1264. }
  1265. rcu_read_unlock();
  1266. return skb->len;
  1267. }
  1268. static struct genl_ops dp_flow_genl_ops[] = {
  1269. { .cmd = OVS_FLOW_CMD_NEW,
  1270. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1271. .policy = flow_policy,
  1272. .doit = ovs_flow_cmd_new_or_set
  1273. },
  1274. { .cmd = OVS_FLOW_CMD_DEL,
  1275. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1276. .policy = flow_policy,
  1277. .doit = ovs_flow_cmd_del
  1278. },
  1279. { .cmd = OVS_FLOW_CMD_GET,
  1280. .flags = 0, /* OK for unprivileged users. */
  1281. .policy = flow_policy,
  1282. .doit = ovs_flow_cmd_get,
  1283. .dumpit = ovs_flow_cmd_dump
  1284. },
  1285. { .cmd = OVS_FLOW_CMD_SET,
  1286. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1287. .policy = flow_policy,
  1288. .doit = ovs_flow_cmd_new_or_set,
  1289. },
  1290. };
  1291. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1292. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1293. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1294. };
  1295. static struct genl_family dp_datapath_genl_family = {
  1296. .id = GENL_ID_GENERATE,
  1297. .hdrsize = sizeof(struct ovs_header),
  1298. .name = OVS_DATAPATH_FAMILY,
  1299. .version = OVS_DATAPATH_VERSION,
  1300. .maxattr = OVS_DP_ATTR_MAX,
  1301. .netnsok = true,
  1302. .parallel_ops = true,
  1303. };
  1304. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  1305. .name = OVS_DATAPATH_MCGROUP
  1306. };
  1307. static size_t ovs_dp_cmd_msg_size(void)
  1308. {
  1309. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1310. msgsize += nla_total_size(IFNAMSIZ);
  1311. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1312. return msgsize;
  1313. }
  1314. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1315. u32 portid, u32 seq, u32 flags, u8 cmd)
  1316. {
  1317. struct ovs_header *ovs_header;
  1318. struct ovs_dp_stats dp_stats;
  1319. int err;
  1320. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1321. flags, cmd);
  1322. if (!ovs_header)
  1323. goto error;
  1324. ovs_header->dp_ifindex = get_dpifindex(dp);
  1325. rcu_read_lock();
  1326. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1327. rcu_read_unlock();
  1328. if (err)
  1329. goto nla_put_failure;
  1330. get_dp_stats(dp, &dp_stats);
  1331. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1332. goto nla_put_failure;
  1333. return genlmsg_end(skb, ovs_header);
  1334. nla_put_failure:
  1335. genlmsg_cancel(skb, ovs_header);
  1336. error:
  1337. return -EMSGSIZE;
  1338. }
  1339. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1340. u32 seq, u8 cmd)
  1341. {
  1342. struct sk_buff *skb;
  1343. int retval;
  1344. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1345. if (!skb)
  1346. return ERR_PTR(-ENOMEM);
  1347. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1348. if (retval < 0) {
  1349. kfree_skb(skb);
  1350. return ERR_PTR(retval);
  1351. }
  1352. return skb;
  1353. }
  1354. /* Called with ovs_mutex. */
  1355. static struct datapath *lookup_datapath(struct net *net,
  1356. struct ovs_header *ovs_header,
  1357. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1358. {
  1359. struct datapath *dp;
  1360. if (!a[OVS_DP_ATTR_NAME])
  1361. dp = get_dp(net, ovs_header->dp_ifindex);
  1362. else {
  1363. struct vport *vport;
  1364. rcu_read_lock();
  1365. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1366. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1367. rcu_read_unlock();
  1368. }
  1369. return dp ? dp : ERR_PTR(-ENODEV);
  1370. }
  1371. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1372. {
  1373. struct nlattr **a = info->attrs;
  1374. struct vport_parms parms;
  1375. struct sk_buff *reply;
  1376. struct datapath *dp;
  1377. struct vport *vport;
  1378. struct ovs_net *ovs_net;
  1379. int err, i;
  1380. err = -EINVAL;
  1381. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1382. goto err;
  1383. ovs_lock();
  1384. err = -ENOMEM;
  1385. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1386. if (dp == NULL)
  1387. goto err_unlock_ovs;
  1388. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1389. /* Allocate table. */
  1390. err = -ENOMEM;
  1391. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1392. if (!dp->table)
  1393. goto err_free_dp;
  1394. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1395. if (!dp->stats_percpu) {
  1396. err = -ENOMEM;
  1397. goto err_destroy_table;
  1398. }
  1399. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1400. GFP_KERNEL);
  1401. if (!dp->ports) {
  1402. err = -ENOMEM;
  1403. goto err_destroy_percpu;
  1404. }
  1405. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1406. INIT_HLIST_HEAD(&dp->ports[i]);
  1407. /* Set up our datapath device. */
  1408. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1409. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1410. parms.options = NULL;
  1411. parms.dp = dp;
  1412. parms.port_no = OVSP_LOCAL;
  1413. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1414. vport = new_vport(&parms);
  1415. if (IS_ERR(vport)) {
  1416. err = PTR_ERR(vport);
  1417. if (err == -EBUSY)
  1418. err = -EEXIST;
  1419. goto err_destroy_ports_array;
  1420. }
  1421. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1422. info->snd_seq, OVS_DP_CMD_NEW);
  1423. err = PTR_ERR(reply);
  1424. if (IS_ERR(reply))
  1425. goto err_destroy_local_port;
  1426. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1427. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1428. ovs_unlock();
  1429. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1430. return 0;
  1431. err_destroy_local_port:
  1432. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1433. err_destroy_ports_array:
  1434. kfree(dp->ports);
  1435. err_destroy_percpu:
  1436. free_percpu(dp->stats_percpu);
  1437. err_destroy_table:
  1438. ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
  1439. err_free_dp:
  1440. release_net(ovs_dp_get_net(dp));
  1441. kfree(dp);
  1442. err_unlock_ovs:
  1443. ovs_unlock();
  1444. err:
  1445. return err;
  1446. }
  1447. /* Called with ovs_mutex. */
  1448. static void __dp_destroy(struct datapath *dp)
  1449. {
  1450. int i;
  1451. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1452. struct vport *vport;
  1453. struct hlist_node *n;
  1454. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1455. if (vport->port_no != OVSP_LOCAL)
  1456. ovs_dp_detach_port(vport);
  1457. }
  1458. list_del_rcu(&dp->list_node);
  1459. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1460. * all port in datapath are destroyed first before freeing datapath.
  1461. */
  1462. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1463. call_rcu(&dp->rcu, destroy_dp_rcu);
  1464. }
  1465. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1466. {
  1467. struct sk_buff *reply;
  1468. struct datapath *dp;
  1469. int err;
  1470. ovs_lock();
  1471. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1472. err = PTR_ERR(dp);
  1473. if (IS_ERR(dp))
  1474. goto unlock;
  1475. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1476. info->snd_seq, OVS_DP_CMD_DEL);
  1477. err = PTR_ERR(reply);
  1478. if (IS_ERR(reply))
  1479. goto unlock;
  1480. __dp_destroy(dp);
  1481. ovs_unlock();
  1482. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1483. return 0;
  1484. unlock:
  1485. ovs_unlock();
  1486. return err;
  1487. }
  1488. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1489. {
  1490. struct sk_buff *reply;
  1491. struct datapath *dp;
  1492. int err;
  1493. ovs_lock();
  1494. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1495. err = PTR_ERR(dp);
  1496. if (IS_ERR(dp))
  1497. goto unlock;
  1498. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1499. info->snd_seq, OVS_DP_CMD_NEW);
  1500. if (IS_ERR(reply)) {
  1501. err = PTR_ERR(reply);
  1502. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1503. ovs_dp_datapath_multicast_group.id, err);
  1504. err = 0;
  1505. goto unlock;
  1506. }
  1507. ovs_unlock();
  1508. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1509. return 0;
  1510. unlock:
  1511. ovs_unlock();
  1512. return err;
  1513. }
  1514. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1515. {
  1516. struct sk_buff *reply;
  1517. struct datapath *dp;
  1518. int err;
  1519. ovs_lock();
  1520. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1521. if (IS_ERR(dp)) {
  1522. err = PTR_ERR(dp);
  1523. goto unlock;
  1524. }
  1525. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1526. info->snd_seq, OVS_DP_CMD_NEW);
  1527. if (IS_ERR(reply)) {
  1528. err = PTR_ERR(reply);
  1529. goto unlock;
  1530. }
  1531. ovs_unlock();
  1532. return genlmsg_reply(reply, info);
  1533. unlock:
  1534. ovs_unlock();
  1535. return err;
  1536. }
  1537. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1538. {
  1539. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1540. struct datapath *dp;
  1541. int skip = cb->args[0];
  1542. int i = 0;
  1543. rcu_read_lock();
  1544. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1545. if (i >= skip &&
  1546. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1547. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1548. OVS_DP_CMD_NEW) < 0)
  1549. break;
  1550. i++;
  1551. }
  1552. rcu_read_unlock();
  1553. cb->args[0] = i;
  1554. return skb->len;
  1555. }
  1556. static struct genl_ops dp_datapath_genl_ops[] = {
  1557. { .cmd = OVS_DP_CMD_NEW,
  1558. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1559. .policy = datapath_policy,
  1560. .doit = ovs_dp_cmd_new
  1561. },
  1562. { .cmd = OVS_DP_CMD_DEL,
  1563. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1564. .policy = datapath_policy,
  1565. .doit = ovs_dp_cmd_del
  1566. },
  1567. { .cmd = OVS_DP_CMD_GET,
  1568. .flags = 0, /* OK for unprivileged users. */
  1569. .policy = datapath_policy,
  1570. .doit = ovs_dp_cmd_get,
  1571. .dumpit = ovs_dp_cmd_dump
  1572. },
  1573. { .cmd = OVS_DP_CMD_SET,
  1574. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1575. .policy = datapath_policy,
  1576. .doit = ovs_dp_cmd_set,
  1577. },
  1578. };
  1579. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1580. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1581. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1582. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1583. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1584. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1585. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1586. };
  1587. static struct genl_family dp_vport_genl_family = {
  1588. .id = GENL_ID_GENERATE,
  1589. .hdrsize = sizeof(struct ovs_header),
  1590. .name = OVS_VPORT_FAMILY,
  1591. .version = OVS_VPORT_VERSION,
  1592. .maxattr = OVS_VPORT_ATTR_MAX,
  1593. .netnsok = true,
  1594. .parallel_ops = true,
  1595. };
  1596. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1597. .name = OVS_VPORT_MCGROUP
  1598. };
  1599. /* Called with ovs_mutex or RCU read lock. */
  1600. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1601. u32 portid, u32 seq, u32 flags, u8 cmd)
  1602. {
  1603. struct ovs_header *ovs_header;
  1604. struct ovs_vport_stats vport_stats;
  1605. int err;
  1606. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1607. flags, cmd);
  1608. if (!ovs_header)
  1609. return -EMSGSIZE;
  1610. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1611. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1612. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1613. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1614. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1615. goto nla_put_failure;
  1616. ovs_vport_get_stats(vport, &vport_stats);
  1617. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1618. &vport_stats))
  1619. goto nla_put_failure;
  1620. err = ovs_vport_get_options(vport, skb);
  1621. if (err == -EMSGSIZE)
  1622. goto error;
  1623. return genlmsg_end(skb, ovs_header);
  1624. nla_put_failure:
  1625. err = -EMSGSIZE;
  1626. error:
  1627. genlmsg_cancel(skb, ovs_header);
  1628. return err;
  1629. }
  1630. /* Called with ovs_mutex or RCU read lock. */
  1631. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1632. u32 seq, u8 cmd)
  1633. {
  1634. struct sk_buff *skb;
  1635. int retval;
  1636. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1637. if (!skb)
  1638. return ERR_PTR(-ENOMEM);
  1639. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1640. BUG_ON(retval < 0);
  1641. return skb;
  1642. }
  1643. /* Called with ovs_mutex or RCU read lock. */
  1644. static struct vport *lookup_vport(struct net *net,
  1645. struct ovs_header *ovs_header,
  1646. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1647. {
  1648. struct datapath *dp;
  1649. struct vport *vport;
  1650. if (a[OVS_VPORT_ATTR_NAME]) {
  1651. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1652. if (!vport)
  1653. return ERR_PTR(-ENODEV);
  1654. if (ovs_header->dp_ifindex &&
  1655. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1656. return ERR_PTR(-ENODEV);
  1657. return vport;
  1658. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1659. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1660. if (port_no >= DP_MAX_PORTS)
  1661. return ERR_PTR(-EFBIG);
  1662. dp = get_dp(net, ovs_header->dp_ifindex);
  1663. if (!dp)
  1664. return ERR_PTR(-ENODEV);
  1665. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1666. if (!vport)
  1667. return ERR_PTR(-ENODEV);
  1668. return vport;
  1669. } else
  1670. return ERR_PTR(-EINVAL);
  1671. }
  1672. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1673. {
  1674. struct nlattr **a = info->attrs;
  1675. struct ovs_header *ovs_header = info->userhdr;
  1676. struct vport_parms parms;
  1677. struct sk_buff *reply;
  1678. struct vport *vport;
  1679. struct datapath *dp;
  1680. u32 port_no;
  1681. int err;
  1682. err = -EINVAL;
  1683. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1684. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1685. goto exit;
  1686. ovs_lock();
  1687. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1688. err = -ENODEV;
  1689. if (!dp)
  1690. goto exit_unlock;
  1691. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1692. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1693. err = -EFBIG;
  1694. if (port_no >= DP_MAX_PORTS)
  1695. goto exit_unlock;
  1696. vport = ovs_vport_ovsl(dp, port_no);
  1697. err = -EBUSY;
  1698. if (vport)
  1699. goto exit_unlock;
  1700. } else {
  1701. for (port_no = 1; ; port_no++) {
  1702. if (port_no >= DP_MAX_PORTS) {
  1703. err = -EFBIG;
  1704. goto exit_unlock;
  1705. }
  1706. vport = ovs_vport_ovsl(dp, port_no);
  1707. if (!vport)
  1708. break;
  1709. }
  1710. }
  1711. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1712. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1713. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1714. parms.dp = dp;
  1715. parms.port_no = port_no;
  1716. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1717. vport = new_vport(&parms);
  1718. err = PTR_ERR(vport);
  1719. if (IS_ERR(vport))
  1720. goto exit_unlock;
  1721. err = 0;
  1722. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1723. OVS_VPORT_CMD_NEW);
  1724. if (IS_ERR(reply)) {
  1725. err = PTR_ERR(reply);
  1726. ovs_dp_detach_port(vport);
  1727. goto exit_unlock;
  1728. }
  1729. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1730. exit_unlock:
  1731. ovs_unlock();
  1732. exit:
  1733. return err;
  1734. }
  1735. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1736. {
  1737. struct nlattr **a = info->attrs;
  1738. struct sk_buff *reply;
  1739. struct vport *vport;
  1740. int err;
  1741. ovs_lock();
  1742. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1743. err = PTR_ERR(vport);
  1744. if (IS_ERR(vport))
  1745. goto exit_unlock;
  1746. if (a[OVS_VPORT_ATTR_TYPE] &&
  1747. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1748. err = -EINVAL;
  1749. goto exit_unlock;
  1750. }
  1751. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1752. if (!reply) {
  1753. err = -ENOMEM;
  1754. goto exit_unlock;
  1755. }
  1756. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1757. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1758. if (err)
  1759. goto exit_free;
  1760. }
  1761. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1762. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1763. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1764. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1765. BUG_ON(err < 0);
  1766. ovs_unlock();
  1767. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1768. return 0;
  1769. exit_free:
  1770. kfree_skb(reply);
  1771. exit_unlock:
  1772. ovs_unlock();
  1773. return err;
  1774. }
  1775. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1776. {
  1777. struct nlattr **a = info->attrs;
  1778. struct sk_buff *reply;
  1779. struct vport *vport;
  1780. int err;
  1781. ovs_lock();
  1782. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1783. err = PTR_ERR(vport);
  1784. if (IS_ERR(vport))
  1785. goto exit_unlock;
  1786. if (vport->port_no == OVSP_LOCAL) {
  1787. err = -EINVAL;
  1788. goto exit_unlock;
  1789. }
  1790. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1791. info->snd_seq, OVS_VPORT_CMD_DEL);
  1792. err = PTR_ERR(reply);
  1793. if (IS_ERR(reply))
  1794. goto exit_unlock;
  1795. err = 0;
  1796. ovs_dp_detach_port(vport);
  1797. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1798. exit_unlock:
  1799. ovs_unlock();
  1800. return err;
  1801. }
  1802. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1803. {
  1804. struct nlattr **a = info->attrs;
  1805. struct ovs_header *ovs_header = info->userhdr;
  1806. struct sk_buff *reply;
  1807. struct vport *vport;
  1808. int err;
  1809. rcu_read_lock();
  1810. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1811. err = PTR_ERR(vport);
  1812. if (IS_ERR(vport))
  1813. goto exit_unlock;
  1814. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1815. info->snd_seq, OVS_VPORT_CMD_NEW);
  1816. err = PTR_ERR(reply);
  1817. if (IS_ERR(reply))
  1818. goto exit_unlock;
  1819. rcu_read_unlock();
  1820. return genlmsg_reply(reply, info);
  1821. exit_unlock:
  1822. rcu_read_unlock();
  1823. return err;
  1824. }
  1825. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1826. {
  1827. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1828. struct datapath *dp;
  1829. int bucket = cb->args[0], skip = cb->args[1];
  1830. int i, j = 0;
  1831. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1832. if (!dp)
  1833. return -ENODEV;
  1834. rcu_read_lock();
  1835. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1836. struct vport *vport;
  1837. j = 0;
  1838. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1839. if (j >= skip &&
  1840. ovs_vport_cmd_fill_info(vport, skb,
  1841. NETLINK_CB(cb->skb).portid,
  1842. cb->nlh->nlmsg_seq,
  1843. NLM_F_MULTI,
  1844. OVS_VPORT_CMD_NEW) < 0)
  1845. goto out;
  1846. j++;
  1847. }
  1848. skip = 0;
  1849. }
  1850. out:
  1851. rcu_read_unlock();
  1852. cb->args[0] = i;
  1853. cb->args[1] = j;
  1854. return skb->len;
  1855. }
  1856. static struct genl_ops dp_vport_genl_ops[] = {
  1857. { .cmd = OVS_VPORT_CMD_NEW,
  1858. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1859. .policy = vport_policy,
  1860. .doit = ovs_vport_cmd_new
  1861. },
  1862. { .cmd = OVS_VPORT_CMD_DEL,
  1863. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1864. .policy = vport_policy,
  1865. .doit = ovs_vport_cmd_del
  1866. },
  1867. { .cmd = OVS_VPORT_CMD_GET,
  1868. .flags = 0, /* OK for unprivileged users. */
  1869. .policy = vport_policy,
  1870. .doit = ovs_vport_cmd_get,
  1871. .dumpit = ovs_vport_cmd_dump
  1872. },
  1873. { .cmd = OVS_VPORT_CMD_SET,
  1874. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1875. .policy = vport_policy,
  1876. .doit = ovs_vport_cmd_set,
  1877. },
  1878. };
  1879. struct genl_family_and_ops {
  1880. struct genl_family *family;
  1881. struct genl_ops *ops;
  1882. int n_ops;
  1883. struct genl_multicast_group *group;
  1884. };
  1885. static const struct genl_family_and_ops dp_genl_families[] = {
  1886. { &dp_datapath_genl_family,
  1887. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1888. &ovs_dp_datapath_multicast_group },
  1889. { &dp_vport_genl_family,
  1890. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1891. &ovs_dp_vport_multicast_group },
  1892. { &dp_flow_genl_family,
  1893. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1894. &ovs_dp_flow_multicast_group },
  1895. { &dp_packet_genl_family,
  1896. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1897. NULL },
  1898. };
  1899. static void dp_unregister_genl(int n_families)
  1900. {
  1901. int i;
  1902. for (i = 0; i < n_families; i++)
  1903. genl_unregister_family(dp_genl_families[i].family);
  1904. }
  1905. static int dp_register_genl(void)
  1906. {
  1907. int n_registered;
  1908. int err;
  1909. int i;
  1910. n_registered = 0;
  1911. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1912. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1913. err = genl_register_family_with_ops(f->family, f->ops,
  1914. f->n_ops);
  1915. if (err)
  1916. goto error;
  1917. n_registered++;
  1918. if (f->group) {
  1919. err = genl_register_mc_group(f->family, f->group);
  1920. if (err)
  1921. goto error;
  1922. }
  1923. }
  1924. return 0;
  1925. error:
  1926. dp_unregister_genl(n_registered);
  1927. return err;
  1928. }
  1929. static void rehash_flow_table(struct work_struct *work)
  1930. {
  1931. struct datapath *dp;
  1932. struct net *net;
  1933. ovs_lock();
  1934. rtnl_lock();
  1935. for_each_net(net) {
  1936. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1937. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1938. struct flow_table *old_table = ovsl_dereference(dp->table);
  1939. struct flow_table *new_table;
  1940. new_table = ovs_flow_tbl_rehash(old_table);
  1941. if (!IS_ERR(new_table)) {
  1942. rcu_assign_pointer(dp->table, new_table);
  1943. ovs_flow_tbl_destroy(old_table, true);
  1944. }
  1945. }
  1946. }
  1947. rtnl_unlock();
  1948. ovs_unlock();
  1949. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1950. }
  1951. static int __net_init ovs_init_net(struct net *net)
  1952. {
  1953. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1954. INIT_LIST_HEAD(&ovs_net->dps);
  1955. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1956. return 0;
  1957. }
  1958. static void __net_exit ovs_exit_net(struct net *net)
  1959. {
  1960. struct datapath *dp, *dp_next;
  1961. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1962. ovs_lock();
  1963. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1964. __dp_destroy(dp);
  1965. ovs_unlock();
  1966. cancel_work_sync(&ovs_net->dp_notify_work);
  1967. }
  1968. static struct pernet_operations ovs_net_ops = {
  1969. .init = ovs_init_net,
  1970. .exit = ovs_exit_net,
  1971. .id = &ovs_net_id,
  1972. .size = sizeof(struct ovs_net),
  1973. };
  1974. static int __init dp_init(void)
  1975. {
  1976. int err;
  1977. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1978. pr_info("Open vSwitch switching datapath\n");
  1979. err = ovs_flow_init();
  1980. if (err)
  1981. goto error;
  1982. err = ovs_vport_init();
  1983. if (err)
  1984. goto error_flow_exit;
  1985. err = register_pernet_device(&ovs_net_ops);
  1986. if (err)
  1987. goto error_vport_exit;
  1988. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1989. if (err)
  1990. goto error_netns_exit;
  1991. err = dp_register_genl();
  1992. if (err < 0)
  1993. goto error_unreg_notifier;
  1994. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1995. return 0;
  1996. error_unreg_notifier:
  1997. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1998. error_netns_exit:
  1999. unregister_pernet_device(&ovs_net_ops);
  2000. error_vport_exit:
  2001. ovs_vport_exit();
  2002. error_flow_exit:
  2003. ovs_flow_exit();
  2004. error:
  2005. return err;
  2006. }
  2007. static void dp_cleanup(void)
  2008. {
  2009. cancel_delayed_work_sync(&rehash_flow_wq);
  2010. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2011. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2012. unregister_pernet_device(&ovs_net_ops);
  2013. rcu_barrier();
  2014. ovs_vport_exit();
  2015. ovs_flow_exit();
  2016. }
  2017. module_init(dp_init);
  2018. module_exit(dp_cleanup);
  2019. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2020. MODULE_LICENSE("GPL");