datapath.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/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/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. unsigned int ovs_net_id __read_mostly;
  58. static struct genl_family dp_packet_genl_family;
  59. static struct genl_family dp_flow_genl_family;
  60. static struct genl_family dp_datapath_genl_family;
  61. static const struct nla_policy flow_policy[];
  62. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  63. .name = OVS_FLOW_MCGROUP,
  64. };
  65. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  66. .name = OVS_DATAPATH_MCGROUP,
  67. };
  68. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  69. .name = OVS_VPORT_MCGROUP,
  70. };
  71. /* Check if need to build a reply message.
  72. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  73. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  74. unsigned int group)
  75. {
  76. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  77. genl_has_listeners(family, genl_info_net(info), group);
  78. }
  79. static void ovs_notify(struct genl_family *family,
  80. struct sk_buff *skb, struct genl_info *info)
  81. {
  82. genl_notify(family, skb, info, 0, GFP_KERNEL);
  83. }
  84. /**
  85. * DOC: Locking:
  86. *
  87. * All writes e.g. Writes to device state (add/remove datapath, port, set
  88. * operations on vports, etc.), Writes to other state (flow table
  89. * modifications, set miscellaneous datapath parameters, etc.) are protected
  90. * by ovs_lock.
  91. *
  92. * Reads are protected by RCU.
  93. *
  94. * There are a few special cases (mostly stats) that have their own
  95. * synchronization but they nest under all of above and don't interact with
  96. * each other.
  97. *
  98. * The RTNL lock nests inside ovs_mutex.
  99. */
  100. static DEFINE_MUTEX(ovs_mutex);
  101. void ovs_lock(void)
  102. {
  103. mutex_lock(&ovs_mutex);
  104. }
  105. void ovs_unlock(void)
  106. {
  107. mutex_unlock(&ovs_mutex);
  108. }
  109. #ifdef CONFIG_LOCKDEP
  110. int lockdep_ovsl_is_held(void)
  111. {
  112. if (debug_locks)
  113. return lockdep_is_held(&ovs_mutex);
  114. else
  115. return 1;
  116. }
  117. #endif
  118. static struct vport *new_vport(const struct vport_parms *);
  119. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  120. const struct sw_flow_key *,
  121. const struct dp_upcall_info *,
  122. uint32_t cutlen);
  123. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  124. const struct sw_flow_key *,
  125. const struct dp_upcall_info *,
  126. uint32_t cutlen);
  127. /* Must be called with rcu_read_lock. */
  128. static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  129. {
  130. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  131. if (dev) {
  132. struct vport *vport = ovs_internal_dev_get_vport(dev);
  133. if (vport)
  134. return vport->dp;
  135. }
  136. return NULL;
  137. }
  138. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  139. * returned dp pointer valid.
  140. */
  141. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  142. {
  143. struct datapath *dp;
  144. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  145. rcu_read_lock();
  146. dp = get_dp_rcu(net, dp_ifindex);
  147. rcu_read_unlock();
  148. return dp;
  149. }
  150. /* Must be called with rcu_read_lock or ovs_mutex. */
  151. const char *ovs_dp_name(const struct datapath *dp)
  152. {
  153. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  154. return ovs_vport_name(vport);
  155. }
  156. static int get_dpifindex(const struct datapath *dp)
  157. {
  158. struct vport *local;
  159. int ifindex;
  160. rcu_read_lock();
  161. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  162. if (local)
  163. ifindex = local->dev->ifindex;
  164. else
  165. ifindex = 0;
  166. rcu_read_unlock();
  167. return ifindex;
  168. }
  169. static void destroy_dp_rcu(struct rcu_head *rcu)
  170. {
  171. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  172. ovs_flow_tbl_destroy(&dp->table);
  173. free_percpu(dp->stats_percpu);
  174. kfree(dp->ports);
  175. kfree(dp);
  176. }
  177. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  178. u16 port_no)
  179. {
  180. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  181. }
  182. /* Called with ovs_mutex or RCU read lock. */
  183. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  184. {
  185. struct vport *vport;
  186. struct hlist_head *head;
  187. head = vport_hash_bucket(dp, port_no);
  188. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  189. if (vport->port_no == port_no)
  190. return vport;
  191. }
  192. return NULL;
  193. }
  194. /* Called with ovs_mutex. */
  195. static struct vport *new_vport(const struct vport_parms *parms)
  196. {
  197. struct vport *vport;
  198. vport = ovs_vport_add(parms);
  199. if (!IS_ERR(vport)) {
  200. struct datapath *dp = parms->dp;
  201. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  202. hlist_add_head_rcu(&vport->dp_hash_node, head);
  203. }
  204. return vport;
  205. }
  206. void ovs_dp_detach_port(struct vport *p)
  207. {
  208. ASSERT_OVSL();
  209. /* First drop references to device. */
  210. hlist_del_rcu(&p->dp_hash_node);
  211. /* Then destroy it. */
  212. ovs_vport_del(p);
  213. }
  214. /* Must be called with rcu_read_lock. */
  215. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  216. {
  217. const struct vport *p = OVS_CB(skb)->input_vport;
  218. struct datapath *dp = p->dp;
  219. struct sw_flow *flow;
  220. struct sw_flow_actions *sf_acts;
  221. struct dp_stats_percpu *stats;
  222. u64 *stats_counter;
  223. u32 n_mask_hit;
  224. stats = this_cpu_ptr(dp->stats_percpu);
  225. /* Look up flow. */
  226. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  227. if (unlikely(!flow)) {
  228. struct dp_upcall_info upcall;
  229. int error;
  230. memset(&upcall, 0, sizeof(upcall));
  231. upcall.cmd = OVS_PACKET_CMD_MISS;
  232. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  233. upcall.mru = OVS_CB(skb)->mru;
  234. error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
  235. if (unlikely(error))
  236. kfree_skb(skb);
  237. else
  238. consume_skb(skb);
  239. stats_counter = &stats->n_missed;
  240. goto out;
  241. }
  242. ovs_flow_stats_update(flow, key->tp.flags, skb);
  243. sf_acts = rcu_dereference(flow->sf_acts);
  244. ovs_execute_actions(dp, skb, sf_acts, key);
  245. stats_counter = &stats->n_hit;
  246. out:
  247. /* Update datapath statistics. */
  248. u64_stats_update_begin(&stats->syncp);
  249. (*stats_counter)++;
  250. stats->n_mask_hit += n_mask_hit;
  251. u64_stats_update_end(&stats->syncp);
  252. }
  253. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  254. const struct sw_flow_key *key,
  255. const struct dp_upcall_info *upcall_info,
  256. uint32_t cutlen)
  257. {
  258. struct dp_stats_percpu *stats;
  259. int err;
  260. if (upcall_info->portid == 0) {
  261. err = -ENOTCONN;
  262. goto err;
  263. }
  264. if (!skb_is_gso(skb))
  265. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  266. else
  267. err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
  268. if (err)
  269. goto err;
  270. return 0;
  271. err:
  272. stats = this_cpu_ptr(dp->stats_percpu);
  273. u64_stats_update_begin(&stats->syncp);
  274. stats->n_lost++;
  275. u64_stats_update_end(&stats->syncp);
  276. return err;
  277. }
  278. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  279. const struct sw_flow_key *key,
  280. const struct dp_upcall_info *upcall_info,
  281. uint32_t cutlen)
  282. {
  283. struct sk_buff *segs, *nskb;
  284. int err;
  285. BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
  286. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  287. if (IS_ERR(segs))
  288. return PTR_ERR(segs);
  289. if (segs == NULL)
  290. return -EINVAL;
  291. /* Queue all of the segments. */
  292. skb = segs;
  293. do {
  294. err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
  295. if (err)
  296. break;
  297. } while ((skb = skb->next));
  298. /* Free all of the segments. */
  299. skb = segs;
  300. do {
  301. nskb = skb->next;
  302. if (err)
  303. kfree_skb(skb);
  304. else
  305. consume_skb(skb);
  306. } while ((skb = nskb));
  307. return err;
  308. }
  309. static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
  310. unsigned int hdrlen)
  311. {
  312. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  313. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  314. + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
  315. + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
  316. /* OVS_PACKET_ATTR_USERDATA */
  317. if (upcall_info->userdata)
  318. size += NLA_ALIGN(upcall_info->userdata->nla_len);
  319. /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
  320. if (upcall_info->egress_tun_info)
  321. size += nla_total_size(ovs_tun_key_attr_size());
  322. /* OVS_PACKET_ATTR_ACTIONS */
  323. if (upcall_info->actions_len)
  324. size += nla_total_size(upcall_info->actions_len);
  325. /* OVS_PACKET_ATTR_MRU */
  326. if (upcall_info->mru)
  327. size += nla_total_size(sizeof(upcall_info->mru));
  328. return size;
  329. }
  330. static void pad_packet(struct datapath *dp, struct sk_buff *skb)
  331. {
  332. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  333. size_t plen = NLA_ALIGN(skb->len) - skb->len;
  334. if (plen > 0)
  335. skb_put_zero(skb, plen);
  336. }
  337. }
  338. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  339. const struct sw_flow_key *key,
  340. const struct dp_upcall_info *upcall_info,
  341. uint32_t cutlen)
  342. {
  343. struct ovs_header *upcall;
  344. struct sk_buff *nskb = NULL;
  345. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  346. struct nlattr *nla;
  347. size_t len;
  348. unsigned int hlen;
  349. int err, dp_ifindex;
  350. dp_ifindex = get_dpifindex(dp);
  351. if (!dp_ifindex)
  352. return -ENODEV;
  353. if (skb_vlan_tag_present(skb)) {
  354. nskb = skb_clone(skb, GFP_ATOMIC);
  355. if (!nskb)
  356. return -ENOMEM;
  357. nskb = __vlan_hwaccel_push_inside(nskb);
  358. if (!nskb)
  359. return -ENOMEM;
  360. skb = nskb;
  361. }
  362. if (nla_attr_size(skb->len) > USHRT_MAX) {
  363. err = -EFBIG;
  364. goto out;
  365. }
  366. /* Complete checksum if needed */
  367. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  368. (err = skb_csum_hwoffload_help(skb, 0)))
  369. goto out;
  370. /* Older versions of OVS user space enforce alignment of the last
  371. * Netlink attribute to NLA_ALIGNTO which would require extensive
  372. * padding logic. Only perform zerocopy if padding is not required.
  373. */
  374. if (dp->user_features & OVS_DP_F_UNALIGNED)
  375. hlen = skb_zerocopy_headlen(skb);
  376. else
  377. hlen = skb->len;
  378. len = upcall_msg_size(upcall_info, hlen - cutlen);
  379. user_skb = genlmsg_new(len, GFP_ATOMIC);
  380. if (!user_skb) {
  381. err = -ENOMEM;
  382. goto out;
  383. }
  384. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  385. 0, upcall_info->cmd);
  386. upcall->dp_ifindex = dp_ifindex;
  387. err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
  388. BUG_ON(err);
  389. if (upcall_info->userdata)
  390. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  391. nla_len(upcall_info->userdata),
  392. nla_data(upcall_info->userdata));
  393. if (upcall_info->egress_tun_info) {
  394. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
  395. err = ovs_nla_put_tunnel_info(user_skb,
  396. upcall_info->egress_tun_info);
  397. BUG_ON(err);
  398. nla_nest_end(user_skb, nla);
  399. }
  400. if (upcall_info->actions_len) {
  401. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
  402. err = ovs_nla_put_actions(upcall_info->actions,
  403. upcall_info->actions_len,
  404. user_skb);
  405. if (!err)
  406. nla_nest_end(user_skb, nla);
  407. else
  408. nla_nest_cancel(user_skb, nla);
  409. }
  410. /* Add OVS_PACKET_ATTR_MRU */
  411. if (upcall_info->mru) {
  412. if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
  413. upcall_info->mru)) {
  414. err = -ENOBUFS;
  415. goto out;
  416. }
  417. pad_packet(dp, user_skb);
  418. }
  419. /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
  420. if (cutlen > 0) {
  421. if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
  422. skb->len)) {
  423. err = -ENOBUFS;
  424. goto out;
  425. }
  426. pad_packet(dp, user_skb);
  427. }
  428. /* Only reserve room for attribute header, packet data is added
  429. * in skb_zerocopy() */
  430. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  431. err = -ENOBUFS;
  432. goto out;
  433. }
  434. nla->nla_len = nla_attr_size(skb->len - cutlen);
  435. err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
  436. if (err)
  437. goto out;
  438. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  439. pad_packet(dp, user_skb);
  440. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  441. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  442. user_skb = NULL;
  443. out:
  444. if (err)
  445. skb_tx_error(skb);
  446. kfree_skb(user_skb);
  447. kfree_skb(nskb);
  448. return err;
  449. }
  450. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  451. {
  452. struct ovs_header *ovs_header = info->userhdr;
  453. struct net *net = sock_net(skb->sk);
  454. struct nlattr **a = info->attrs;
  455. struct sw_flow_actions *acts;
  456. struct sk_buff *packet;
  457. struct sw_flow *flow;
  458. struct sw_flow_actions *sf_acts;
  459. struct datapath *dp;
  460. struct vport *input_vport;
  461. u16 mru = 0;
  462. int len;
  463. int err;
  464. bool log = !a[OVS_PACKET_ATTR_PROBE];
  465. err = -EINVAL;
  466. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  467. !a[OVS_PACKET_ATTR_ACTIONS])
  468. goto err;
  469. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  470. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  471. err = -ENOMEM;
  472. if (!packet)
  473. goto err;
  474. skb_reserve(packet, NET_IP_ALIGN);
  475. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  476. /* Set packet's mru */
  477. if (a[OVS_PACKET_ATTR_MRU]) {
  478. mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
  479. packet->ignore_df = 1;
  480. }
  481. OVS_CB(packet)->mru = mru;
  482. /* Build an sw_flow for sending this packet. */
  483. flow = ovs_flow_alloc();
  484. err = PTR_ERR(flow);
  485. if (IS_ERR(flow))
  486. goto err_kfree_skb;
  487. err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
  488. packet, &flow->key, log);
  489. if (err)
  490. goto err_flow_free;
  491. err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
  492. &flow->key, &acts, log);
  493. if (err)
  494. goto err_flow_free;
  495. rcu_assign_pointer(flow->sf_acts, acts);
  496. packet->priority = flow->key.phy.priority;
  497. packet->mark = flow->key.phy.skb_mark;
  498. rcu_read_lock();
  499. dp = get_dp_rcu(net, ovs_header->dp_ifindex);
  500. err = -ENODEV;
  501. if (!dp)
  502. goto err_unlock;
  503. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  504. if (!input_vport)
  505. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  506. if (!input_vport)
  507. goto err_unlock;
  508. packet->dev = input_vport->dev;
  509. OVS_CB(packet)->input_vport = input_vport;
  510. sf_acts = rcu_dereference(flow->sf_acts);
  511. local_bh_disable();
  512. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  513. local_bh_enable();
  514. rcu_read_unlock();
  515. ovs_flow_free(flow, false);
  516. return err;
  517. err_unlock:
  518. rcu_read_unlock();
  519. err_flow_free:
  520. ovs_flow_free(flow, false);
  521. err_kfree_skb:
  522. kfree_skb(packet);
  523. err:
  524. return err;
  525. }
  526. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  527. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  528. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  529. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  530. [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
  531. [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
  532. };
  533. static const struct genl_ops dp_packet_genl_ops[] = {
  534. { .cmd = OVS_PACKET_CMD_EXECUTE,
  535. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  536. .policy = packet_policy,
  537. .doit = ovs_packet_cmd_execute
  538. }
  539. };
  540. static struct genl_family dp_packet_genl_family __ro_after_init = {
  541. .hdrsize = sizeof(struct ovs_header),
  542. .name = OVS_PACKET_FAMILY,
  543. .version = OVS_PACKET_VERSION,
  544. .maxattr = OVS_PACKET_ATTR_MAX,
  545. .netnsok = true,
  546. .parallel_ops = true,
  547. .ops = dp_packet_genl_ops,
  548. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  549. .module = THIS_MODULE,
  550. };
  551. static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
  552. struct ovs_dp_megaflow_stats *mega_stats)
  553. {
  554. int i;
  555. memset(mega_stats, 0, sizeof(*mega_stats));
  556. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  557. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  558. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  559. for_each_possible_cpu(i) {
  560. const struct dp_stats_percpu *percpu_stats;
  561. struct dp_stats_percpu local_stats;
  562. unsigned int start;
  563. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  564. do {
  565. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  566. local_stats = *percpu_stats;
  567. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  568. stats->n_hit += local_stats.n_hit;
  569. stats->n_missed += local_stats.n_missed;
  570. stats->n_lost += local_stats.n_lost;
  571. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  572. }
  573. }
  574. static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
  575. {
  576. return ovs_identifier_is_ufid(sfid) &&
  577. !(ufid_flags & OVS_UFID_F_OMIT_KEY);
  578. }
  579. static bool should_fill_mask(uint32_t ufid_flags)
  580. {
  581. return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
  582. }
  583. static bool should_fill_actions(uint32_t ufid_flags)
  584. {
  585. return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
  586. }
  587. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
  588. const struct sw_flow_id *sfid,
  589. uint32_t ufid_flags)
  590. {
  591. size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
  592. /* OVS_FLOW_ATTR_UFID */
  593. if (sfid && ovs_identifier_is_ufid(sfid))
  594. len += nla_total_size(sfid->ufid_len);
  595. /* OVS_FLOW_ATTR_KEY */
  596. if (!sfid || should_fill_key(sfid, ufid_flags))
  597. len += nla_total_size(ovs_key_attr_size());
  598. /* OVS_FLOW_ATTR_MASK */
  599. if (should_fill_mask(ufid_flags))
  600. len += nla_total_size(ovs_key_attr_size());
  601. /* OVS_FLOW_ATTR_ACTIONS */
  602. if (should_fill_actions(ufid_flags))
  603. len += nla_total_size(acts->orig_len);
  604. return len
  605. + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  606. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  607. + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
  608. }
  609. /* Called with ovs_mutex or RCU read lock. */
  610. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  611. struct sk_buff *skb)
  612. {
  613. struct ovs_flow_stats stats;
  614. __be16 tcp_flags;
  615. unsigned long used;
  616. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  617. if (used &&
  618. nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
  619. OVS_FLOW_ATTR_PAD))
  620. return -EMSGSIZE;
  621. if (stats.n_packets &&
  622. nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
  623. sizeof(struct ovs_flow_stats), &stats,
  624. OVS_FLOW_ATTR_PAD))
  625. return -EMSGSIZE;
  626. if ((u8)ntohs(tcp_flags) &&
  627. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  628. return -EMSGSIZE;
  629. return 0;
  630. }
  631. /* Called with ovs_mutex or RCU read lock. */
  632. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  633. struct sk_buff *skb, int skb_orig_len)
  634. {
  635. struct nlattr *start;
  636. int err;
  637. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  638. * this is the first flow to be dumped into 'skb'. This is unusual for
  639. * Netlink but individual action lists can be longer than
  640. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  641. * The userspace caller can always fetch the actions separately if it
  642. * really wants them. (Most userspace callers in fact don't care.)
  643. *
  644. * This can only fail for dump operations because the skb is always
  645. * properly sized for single flows.
  646. */
  647. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  648. if (start) {
  649. const struct sw_flow_actions *sf_acts;
  650. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  651. err = ovs_nla_put_actions(sf_acts->actions,
  652. sf_acts->actions_len, skb);
  653. if (!err)
  654. nla_nest_end(skb, start);
  655. else {
  656. if (skb_orig_len)
  657. return err;
  658. nla_nest_cancel(skb, start);
  659. }
  660. } else if (skb_orig_len) {
  661. return -EMSGSIZE;
  662. }
  663. return 0;
  664. }
  665. /* Called with ovs_mutex or RCU read lock. */
  666. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  667. struct sk_buff *skb, u32 portid,
  668. u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
  669. {
  670. const int skb_orig_len = skb->len;
  671. struct ovs_header *ovs_header;
  672. int err;
  673. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  674. flags, cmd);
  675. if (!ovs_header)
  676. return -EMSGSIZE;
  677. ovs_header->dp_ifindex = dp_ifindex;
  678. err = ovs_nla_put_identifier(flow, skb);
  679. if (err)
  680. goto error;
  681. if (should_fill_key(&flow->id, ufid_flags)) {
  682. err = ovs_nla_put_masked_key(flow, skb);
  683. if (err)
  684. goto error;
  685. }
  686. if (should_fill_mask(ufid_flags)) {
  687. err = ovs_nla_put_mask(flow, skb);
  688. if (err)
  689. goto error;
  690. }
  691. err = ovs_flow_cmd_fill_stats(flow, skb);
  692. if (err)
  693. goto error;
  694. if (should_fill_actions(ufid_flags)) {
  695. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  696. if (err)
  697. goto error;
  698. }
  699. genlmsg_end(skb, ovs_header);
  700. return 0;
  701. error:
  702. genlmsg_cancel(skb, ovs_header);
  703. return err;
  704. }
  705. /* May not be called with RCU read lock. */
  706. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  707. const struct sw_flow_id *sfid,
  708. struct genl_info *info,
  709. bool always,
  710. uint32_t ufid_flags)
  711. {
  712. struct sk_buff *skb;
  713. size_t len;
  714. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  715. return NULL;
  716. len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
  717. skb = genlmsg_new(len, GFP_KERNEL);
  718. if (!skb)
  719. return ERR_PTR(-ENOMEM);
  720. return skb;
  721. }
  722. /* Called with ovs_mutex. */
  723. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  724. int dp_ifindex,
  725. struct genl_info *info, u8 cmd,
  726. bool always, u32 ufid_flags)
  727. {
  728. struct sk_buff *skb;
  729. int retval;
  730. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
  731. &flow->id, info, always, ufid_flags);
  732. if (IS_ERR_OR_NULL(skb))
  733. return skb;
  734. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  735. info->snd_portid, info->snd_seq, 0,
  736. cmd, ufid_flags);
  737. BUG_ON(retval < 0);
  738. return skb;
  739. }
  740. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  741. {
  742. struct net *net = sock_net(skb->sk);
  743. struct nlattr **a = info->attrs;
  744. struct ovs_header *ovs_header = info->userhdr;
  745. struct sw_flow *flow = NULL, *new_flow;
  746. struct sw_flow_mask mask;
  747. struct sk_buff *reply;
  748. struct datapath *dp;
  749. struct sw_flow_actions *acts;
  750. struct sw_flow_match match;
  751. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  752. int error;
  753. bool log = !a[OVS_FLOW_ATTR_PROBE];
  754. /* Must have key and actions. */
  755. error = -EINVAL;
  756. if (!a[OVS_FLOW_ATTR_KEY]) {
  757. OVS_NLERR(log, "Flow key attr not present in new flow.");
  758. goto error;
  759. }
  760. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  761. OVS_NLERR(log, "Flow actions attr not present in new flow.");
  762. goto error;
  763. }
  764. /* Most of the time we need to allocate a new flow, do it before
  765. * locking.
  766. */
  767. new_flow = ovs_flow_alloc();
  768. if (IS_ERR(new_flow)) {
  769. error = PTR_ERR(new_flow);
  770. goto error;
  771. }
  772. /* Extract key. */
  773. ovs_match_init(&match, &new_flow->key, false, &mask);
  774. error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  775. a[OVS_FLOW_ATTR_MASK], log);
  776. if (error)
  777. goto err_kfree_flow;
  778. /* Extract flow identifier. */
  779. error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
  780. &new_flow->key, log);
  781. if (error)
  782. goto err_kfree_flow;
  783. /* unmasked key is needed to match when ufid is not used. */
  784. if (ovs_identifier_is_key(&new_flow->id))
  785. match.key = new_flow->id.unmasked_key;
  786. ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
  787. /* Validate actions. */
  788. error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
  789. &new_flow->key, &acts, log);
  790. if (error) {
  791. OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
  792. goto err_kfree_flow;
  793. }
  794. reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
  795. ufid_flags);
  796. if (IS_ERR(reply)) {
  797. error = PTR_ERR(reply);
  798. goto err_kfree_acts;
  799. }
  800. ovs_lock();
  801. dp = get_dp(net, ovs_header->dp_ifindex);
  802. if (unlikely(!dp)) {
  803. error = -ENODEV;
  804. goto err_unlock_ovs;
  805. }
  806. /* Check if this is a duplicate flow */
  807. if (ovs_identifier_is_ufid(&new_flow->id))
  808. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
  809. if (!flow)
  810. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
  811. if (likely(!flow)) {
  812. rcu_assign_pointer(new_flow->sf_acts, acts);
  813. /* Put flow in bucket. */
  814. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  815. if (unlikely(error)) {
  816. acts = NULL;
  817. goto err_unlock_ovs;
  818. }
  819. if (unlikely(reply)) {
  820. error = ovs_flow_cmd_fill_info(new_flow,
  821. ovs_header->dp_ifindex,
  822. reply, info->snd_portid,
  823. info->snd_seq, 0,
  824. OVS_FLOW_CMD_NEW,
  825. ufid_flags);
  826. BUG_ON(error < 0);
  827. }
  828. ovs_unlock();
  829. } else {
  830. struct sw_flow_actions *old_acts;
  831. /* Bail out if we're not allowed to modify an existing flow.
  832. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  833. * because Generic Netlink treats the latter as a dump
  834. * request. We also accept NLM_F_EXCL in case that bug ever
  835. * gets fixed.
  836. */
  837. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  838. | NLM_F_EXCL))) {
  839. error = -EEXIST;
  840. goto err_unlock_ovs;
  841. }
  842. /* The flow identifier has to be the same for flow updates.
  843. * Look for any overlapping flow.
  844. */
  845. if (unlikely(!ovs_flow_cmp(flow, &match))) {
  846. if (ovs_identifier_is_key(&flow->id))
  847. flow = ovs_flow_tbl_lookup_exact(&dp->table,
  848. &match);
  849. else /* UFID matches but key is different */
  850. flow = NULL;
  851. if (!flow) {
  852. error = -ENOENT;
  853. goto err_unlock_ovs;
  854. }
  855. }
  856. /* Update actions. */
  857. old_acts = ovsl_dereference(flow->sf_acts);
  858. rcu_assign_pointer(flow->sf_acts, acts);
  859. if (unlikely(reply)) {
  860. error = ovs_flow_cmd_fill_info(flow,
  861. ovs_header->dp_ifindex,
  862. reply, info->snd_portid,
  863. info->snd_seq, 0,
  864. OVS_FLOW_CMD_NEW,
  865. ufid_flags);
  866. BUG_ON(error < 0);
  867. }
  868. ovs_unlock();
  869. ovs_nla_free_flow_actions_rcu(old_acts);
  870. ovs_flow_free(new_flow, false);
  871. }
  872. if (reply)
  873. ovs_notify(&dp_flow_genl_family, reply, info);
  874. return 0;
  875. err_unlock_ovs:
  876. ovs_unlock();
  877. kfree_skb(reply);
  878. err_kfree_acts:
  879. ovs_nla_free_flow_actions(acts);
  880. err_kfree_flow:
  881. ovs_flow_free(new_flow, false);
  882. error:
  883. return error;
  884. }
  885. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  886. static struct sw_flow_actions *get_flow_actions(struct net *net,
  887. const struct nlattr *a,
  888. const struct sw_flow_key *key,
  889. const struct sw_flow_mask *mask,
  890. bool log)
  891. {
  892. struct sw_flow_actions *acts;
  893. struct sw_flow_key masked_key;
  894. int error;
  895. ovs_flow_mask_key(&masked_key, key, true, mask);
  896. error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
  897. if (error) {
  898. OVS_NLERR(log,
  899. "Actions may not be safe on all matching packets");
  900. return ERR_PTR(error);
  901. }
  902. return acts;
  903. }
  904. /* Factor out match-init and action-copy to avoid
  905. * "Wframe-larger-than=1024" warning. Because mask is only
  906. * used to get actions, we new a function to save some
  907. * stack space.
  908. *
  909. * If there are not key and action attrs, we return 0
  910. * directly. In the case, the caller will also not use the
  911. * match as before. If there is action attr, we try to get
  912. * actions and save them to *acts. Before returning from
  913. * the function, we reset the match->mask pointer. Because
  914. * we should not to return match object with dangling reference
  915. * to mask.
  916. * */
  917. static int ovs_nla_init_match_and_action(struct net *net,
  918. struct sw_flow_match *match,
  919. struct sw_flow_key *key,
  920. struct nlattr **a,
  921. struct sw_flow_actions **acts,
  922. bool log)
  923. {
  924. struct sw_flow_mask mask;
  925. int error = 0;
  926. if (a[OVS_FLOW_ATTR_KEY]) {
  927. ovs_match_init(match, key, true, &mask);
  928. error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
  929. a[OVS_FLOW_ATTR_MASK], log);
  930. if (error)
  931. goto error;
  932. }
  933. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  934. if (!a[OVS_FLOW_ATTR_KEY]) {
  935. OVS_NLERR(log,
  936. "Flow key attribute not present in set flow.");
  937. return -EINVAL;
  938. }
  939. *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
  940. &mask, log);
  941. if (IS_ERR(*acts)) {
  942. error = PTR_ERR(*acts);
  943. goto error;
  944. }
  945. }
  946. /* On success, error is 0. */
  947. error:
  948. match->mask = NULL;
  949. return error;
  950. }
  951. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  952. {
  953. struct net *net = sock_net(skb->sk);
  954. struct nlattr **a = info->attrs;
  955. struct ovs_header *ovs_header = info->userhdr;
  956. struct sw_flow_key key;
  957. struct sw_flow *flow;
  958. struct sk_buff *reply = NULL;
  959. struct datapath *dp;
  960. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  961. struct sw_flow_match match;
  962. struct sw_flow_id sfid;
  963. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  964. int error = 0;
  965. bool log = !a[OVS_FLOW_ATTR_PROBE];
  966. bool ufid_present;
  967. ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
  968. if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
  969. OVS_NLERR(log,
  970. "Flow set message rejected, Key attribute missing.");
  971. return -EINVAL;
  972. }
  973. error = ovs_nla_init_match_and_action(net, &match, &key, a,
  974. &acts, log);
  975. if (error)
  976. goto error;
  977. if (acts) {
  978. /* Can allocate before locking if have acts. */
  979. reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
  980. ufid_flags);
  981. if (IS_ERR(reply)) {
  982. error = PTR_ERR(reply);
  983. goto err_kfree_acts;
  984. }
  985. }
  986. ovs_lock();
  987. dp = get_dp(net, ovs_header->dp_ifindex);
  988. if (unlikely(!dp)) {
  989. error = -ENODEV;
  990. goto err_unlock_ovs;
  991. }
  992. /* Check that the flow exists. */
  993. if (ufid_present)
  994. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
  995. else
  996. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  997. if (unlikely(!flow)) {
  998. error = -ENOENT;
  999. goto err_unlock_ovs;
  1000. }
  1001. /* Update actions, if present. */
  1002. if (likely(acts)) {
  1003. old_acts = ovsl_dereference(flow->sf_acts);
  1004. rcu_assign_pointer(flow->sf_acts, acts);
  1005. if (unlikely(reply)) {
  1006. error = ovs_flow_cmd_fill_info(flow,
  1007. ovs_header->dp_ifindex,
  1008. reply, info->snd_portid,
  1009. info->snd_seq, 0,
  1010. OVS_FLOW_CMD_NEW,
  1011. ufid_flags);
  1012. BUG_ON(error < 0);
  1013. }
  1014. } else {
  1015. /* Could not alloc without acts before locking. */
  1016. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  1017. info, OVS_FLOW_CMD_NEW, false,
  1018. ufid_flags);
  1019. if (IS_ERR(reply)) {
  1020. error = PTR_ERR(reply);
  1021. goto err_unlock_ovs;
  1022. }
  1023. }
  1024. /* Clear stats. */
  1025. if (a[OVS_FLOW_ATTR_CLEAR])
  1026. ovs_flow_stats_clear(flow);
  1027. ovs_unlock();
  1028. if (reply)
  1029. ovs_notify(&dp_flow_genl_family, reply, info);
  1030. if (old_acts)
  1031. ovs_nla_free_flow_actions_rcu(old_acts);
  1032. return 0;
  1033. err_unlock_ovs:
  1034. ovs_unlock();
  1035. kfree_skb(reply);
  1036. err_kfree_acts:
  1037. ovs_nla_free_flow_actions(acts);
  1038. error:
  1039. return error;
  1040. }
  1041. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1042. {
  1043. struct nlattr **a = info->attrs;
  1044. struct ovs_header *ovs_header = info->userhdr;
  1045. struct net *net = sock_net(skb->sk);
  1046. struct sw_flow_key key;
  1047. struct sk_buff *reply;
  1048. struct sw_flow *flow;
  1049. struct datapath *dp;
  1050. struct sw_flow_match match;
  1051. struct sw_flow_id ufid;
  1052. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1053. int err = 0;
  1054. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1055. bool ufid_present;
  1056. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1057. if (a[OVS_FLOW_ATTR_KEY]) {
  1058. ovs_match_init(&match, &key, true, NULL);
  1059. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
  1060. log);
  1061. } else if (!ufid_present) {
  1062. OVS_NLERR(log,
  1063. "Flow get message rejected, Key attribute missing.");
  1064. err = -EINVAL;
  1065. }
  1066. if (err)
  1067. return err;
  1068. ovs_lock();
  1069. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1070. if (!dp) {
  1071. err = -ENODEV;
  1072. goto unlock;
  1073. }
  1074. if (ufid_present)
  1075. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1076. else
  1077. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1078. if (!flow) {
  1079. err = -ENOENT;
  1080. goto unlock;
  1081. }
  1082. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  1083. OVS_FLOW_CMD_NEW, true, ufid_flags);
  1084. if (IS_ERR(reply)) {
  1085. err = PTR_ERR(reply);
  1086. goto unlock;
  1087. }
  1088. ovs_unlock();
  1089. return genlmsg_reply(reply, info);
  1090. unlock:
  1091. ovs_unlock();
  1092. return err;
  1093. }
  1094. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1095. {
  1096. struct nlattr **a = info->attrs;
  1097. struct ovs_header *ovs_header = info->userhdr;
  1098. struct net *net = sock_net(skb->sk);
  1099. struct sw_flow_key key;
  1100. struct sk_buff *reply;
  1101. struct sw_flow *flow = NULL;
  1102. struct datapath *dp;
  1103. struct sw_flow_match match;
  1104. struct sw_flow_id ufid;
  1105. u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1106. int err;
  1107. bool log = !a[OVS_FLOW_ATTR_PROBE];
  1108. bool ufid_present;
  1109. ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
  1110. if (a[OVS_FLOW_ATTR_KEY]) {
  1111. ovs_match_init(&match, &key, true, NULL);
  1112. err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
  1113. NULL, log);
  1114. if (unlikely(err))
  1115. return err;
  1116. }
  1117. ovs_lock();
  1118. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1119. if (unlikely(!dp)) {
  1120. err = -ENODEV;
  1121. goto unlock;
  1122. }
  1123. if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
  1124. err = ovs_flow_tbl_flush(&dp->table);
  1125. goto unlock;
  1126. }
  1127. if (ufid_present)
  1128. flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
  1129. else
  1130. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  1131. if (unlikely(!flow)) {
  1132. err = -ENOENT;
  1133. goto unlock;
  1134. }
  1135. ovs_flow_tbl_remove(&dp->table, flow);
  1136. ovs_unlock();
  1137. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  1138. &flow->id, info, false, ufid_flags);
  1139. if (likely(reply)) {
  1140. if (likely(!IS_ERR(reply))) {
  1141. rcu_read_lock(); /*To keep RCU checker happy. */
  1142. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  1143. reply, info->snd_portid,
  1144. info->snd_seq, 0,
  1145. OVS_FLOW_CMD_DEL,
  1146. ufid_flags);
  1147. rcu_read_unlock();
  1148. BUG_ON(err < 0);
  1149. ovs_notify(&dp_flow_genl_family, reply, info);
  1150. } else {
  1151. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  1152. }
  1153. }
  1154. ovs_flow_free(flow, true);
  1155. return 0;
  1156. unlock:
  1157. ovs_unlock();
  1158. return err;
  1159. }
  1160. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1161. {
  1162. struct nlattr *a[__OVS_FLOW_ATTR_MAX];
  1163. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1164. struct table_instance *ti;
  1165. struct datapath *dp;
  1166. u32 ufid_flags;
  1167. int err;
  1168. err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
  1169. OVS_FLOW_ATTR_MAX, flow_policy, NULL);
  1170. if (err)
  1171. return err;
  1172. ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
  1173. rcu_read_lock();
  1174. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1175. if (!dp) {
  1176. rcu_read_unlock();
  1177. return -ENODEV;
  1178. }
  1179. ti = rcu_dereference(dp->table.ti);
  1180. for (;;) {
  1181. struct sw_flow *flow;
  1182. u32 bucket, obj;
  1183. bucket = cb->args[0];
  1184. obj = cb->args[1];
  1185. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1186. if (!flow)
  1187. break;
  1188. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1189. NETLINK_CB(cb->skb).portid,
  1190. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1191. OVS_FLOW_CMD_NEW, ufid_flags) < 0)
  1192. break;
  1193. cb->args[0] = bucket;
  1194. cb->args[1] = obj;
  1195. }
  1196. rcu_read_unlock();
  1197. return skb->len;
  1198. }
  1199. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1200. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1201. [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
  1202. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1203. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1204. [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
  1205. [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
  1206. [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
  1207. };
  1208. static const struct genl_ops dp_flow_genl_ops[] = {
  1209. { .cmd = OVS_FLOW_CMD_NEW,
  1210. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1211. .policy = flow_policy,
  1212. .doit = ovs_flow_cmd_new
  1213. },
  1214. { .cmd = OVS_FLOW_CMD_DEL,
  1215. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1216. .policy = flow_policy,
  1217. .doit = ovs_flow_cmd_del
  1218. },
  1219. { .cmd = OVS_FLOW_CMD_GET,
  1220. .flags = 0, /* OK for unprivileged users. */
  1221. .policy = flow_policy,
  1222. .doit = ovs_flow_cmd_get,
  1223. .dumpit = ovs_flow_cmd_dump
  1224. },
  1225. { .cmd = OVS_FLOW_CMD_SET,
  1226. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1227. .policy = flow_policy,
  1228. .doit = ovs_flow_cmd_set,
  1229. },
  1230. };
  1231. static struct genl_family dp_flow_genl_family __ro_after_init = {
  1232. .hdrsize = sizeof(struct ovs_header),
  1233. .name = OVS_FLOW_FAMILY,
  1234. .version = OVS_FLOW_VERSION,
  1235. .maxattr = OVS_FLOW_ATTR_MAX,
  1236. .netnsok = true,
  1237. .parallel_ops = true,
  1238. .ops = dp_flow_genl_ops,
  1239. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1240. .mcgrps = &ovs_dp_flow_multicast_group,
  1241. .n_mcgrps = 1,
  1242. .module = THIS_MODULE,
  1243. };
  1244. static size_t ovs_dp_cmd_msg_size(void)
  1245. {
  1246. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1247. msgsize += nla_total_size(IFNAMSIZ);
  1248. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
  1249. msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
  1250. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1251. return msgsize;
  1252. }
  1253. /* Called with ovs_mutex. */
  1254. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1255. u32 portid, u32 seq, u32 flags, u8 cmd)
  1256. {
  1257. struct ovs_header *ovs_header;
  1258. struct ovs_dp_stats dp_stats;
  1259. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1260. int err;
  1261. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1262. flags, cmd);
  1263. if (!ovs_header)
  1264. goto error;
  1265. ovs_header->dp_ifindex = get_dpifindex(dp);
  1266. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1267. if (err)
  1268. goto nla_put_failure;
  1269. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1270. if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1271. &dp_stats, OVS_DP_ATTR_PAD))
  1272. goto nla_put_failure;
  1273. if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1274. sizeof(struct ovs_dp_megaflow_stats),
  1275. &dp_megaflow_stats, OVS_DP_ATTR_PAD))
  1276. goto nla_put_failure;
  1277. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1278. goto nla_put_failure;
  1279. genlmsg_end(skb, ovs_header);
  1280. return 0;
  1281. nla_put_failure:
  1282. genlmsg_cancel(skb, ovs_header);
  1283. error:
  1284. return -EMSGSIZE;
  1285. }
  1286. static struct sk_buff *ovs_dp_cmd_alloc_info(void)
  1287. {
  1288. return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1289. }
  1290. /* Called with rcu_read_lock or ovs_mutex. */
  1291. static struct datapath *lookup_datapath(struct net *net,
  1292. const struct ovs_header *ovs_header,
  1293. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1294. {
  1295. struct datapath *dp;
  1296. if (!a[OVS_DP_ATTR_NAME])
  1297. dp = get_dp(net, ovs_header->dp_ifindex);
  1298. else {
  1299. struct vport *vport;
  1300. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1301. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1302. }
  1303. return dp ? dp : ERR_PTR(-ENODEV);
  1304. }
  1305. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1306. {
  1307. struct datapath *dp;
  1308. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1309. if (IS_ERR(dp))
  1310. return;
  1311. WARN(dp->user_features, "Dropping previously announced user features\n");
  1312. dp->user_features = 0;
  1313. }
  1314. static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
  1315. {
  1316. if (a[OVS_DP_ATTR_USER_FEATURES])
  1317. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1318. }
  1319. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1320. {
  1321. struct nlattr **a = info->attrs;
  1322. struct vport_parms parms;
  1323. struct sk_buff *reply;
  1324. struct datapath *dp;
  1325. struct vport *vport;
  1326. struct ovs_net *ovs_net;
  1327. int err, i;
  1328. err = -EINVAL;
  1329. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1330. goto err;
  1331. reply = ovs_dp_cmd_alloc_info();
  1332. if (!reply)
  1333. return -ENOMEM;
  1334. err = -ENOMEM;
  1335. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1336. if (dp == NULL)
  1337. goto err_free_reply;
  1338. ovs_dp_set_net(dp, sock_net(skb->sk));
  1339. /* Allocate table. */
  1340. err = ovs_flow_tbl_init(&dp->table);
  1341. if (err)
  1342. goto err_free_dp;
  1343. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1344. if (!dp->stats_percpu) {
  1345. err = -ENOMEM;
  1346. goto err_destroy_table;
  1347. }
  1348. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1349. GFP_KERNEL);
  1350. if (!dp->ports) {
  1351. err = -ENOMEM;
  1352. goto err_destroy_percpu;
  1353. }
  1354. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1355. INIT_HLIST_HEAD(&dp->ports[i]);
  1356. /* Set up our datapath device. */
  1357. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1358. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1359. parms.options = NULL;
  1360. parms.dp = dp;
  1361. parms.port_no = OVSP_LOCAL;
  1362. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1363. ovs_dp_change(dp, a);
  1364. /* So far only local changes have been made, now need the lock. */
  1365. ovs_lock();
  1366. vport = new_vport(&parms);
  1367. if (IS_ERR(vport)) {
  1368. err = PTR_ERR(vport);
  1369. if (err == -EBUSY)
  1370. err = -EEXIST;
  1371. if (err == -EEXIST) {
  1372. /* An outdated user space instance that does not understand
  1373. * the concept of user_features has attempted to create a new
  1374. * datapath and is likely to reuse it. Drop all user features.
  1375. */
  1376. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1377. ovs_dp_reset_user_features(skb, info);
  1378. }
  1379. goto err_destroy_ports_array;
  1380. }
  1381. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1382. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1383. BUG_ON(err < 0);
  1384. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1385. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1386. ovs_unlock();
  1387. ovs_notify(&dp_datapath_genl_family, reply, info);
  1388. return 0;
  1389. err_destroy_ports_array:
  1390. ovs_unlock();
  1391. kfree(dp->ports);
  1392. err_destroy_percpu:
  1393. free_percpu(dp->stats_percpu);
  1394. err_destroy_table:
  1395. ovs_flow_tbl_destroy(&dp->table);
  1396. err_free_dp:
  1397. kfree(dp);
  1398. err_free_reply:
  1399. kfree_skb(reply);
  1400. err:
  1401. return err;
  1402. }
  1403. /* Called with ovs_mutex. */
  1404. static void __dp_destroy(struct datapath *dp)
  1405. {
  1406. int i;
  1407. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1408. struct vport *vport;
  1409. struct hlist_node *n;
  1410. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1411. if (vport->port_no != OVSP_LOCAL)
  1412. ovs_dp_detach_port(vport);
  1413. }
  1414. list_del_rcu(&dp->list_node);
  1415. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1416. * all ports in datapath are destroyed first before freeing datapath.
  1417. */
  1418. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1419. /* RCU destroy the flow table */
  1420. call_rcu(&dp->rcu, destroy_dp_rcu);
  1421. }
  1422. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1423. {
  1424. struct sk_buff *reply;
  1425. struct datapath *dp;
  1426. int err;
  1427. reply = ovs_dp_cmd_alloc_info();
  1428. if (!reply)
  1429. return -ENOMEM;
  1430. ovs_lock();
  1431. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1432. err = PTR_ERR(dp);
  1433. if (IS_ERR(dp))
  1434. goto err_unlock_free;
  1435. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1436. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1437. BUG_ON(err < 0);
  1438. __dp_destroy(dp);
  1439. ovs_unlock();
  1440. ovs_notify(&dp_datapath_genl_family, reply, info);
  1441. return 0;
  1442. err_unlock_free:
  1443. ovs_unlock();
  1444. kfree_skb(reply);
  1445. return err;
  1446. }
  1447. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1448. {
  1449. struct sk_buff *reply;
  1450. struct datapath *dp;
  1451. int err;
  1452. reply = ovs_dp_cmd_alloc_info();
  1453. if (!reply)
  1454. return -ENOMEM;
  1455. ovs_lock();
  1456. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1457. err = PTR_ERR(dp);
  1458. if (IS_ERR(dp))
  1459. goto err_unlock_free;
  1460. ovs_dp_change(dp, info->attrs);
  1461. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1462. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1463. BUG_ON(err < 0);
  1464. ovs_unlock();
  1465. ovs_notify(&dp_datapath_genl_family, reply, info);
  1466. return 0;
  1467. err_unlock_free:
  1468. ovs_unlock();
  1469. kfree_skb(reply);
  1470. return err;
  1471. }
  1472. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1473. {
  1474. struct sk_buff *reply;
  1475. struct datapath *dp;
  1476. int err;
  1477. reply = ovs_dp_cmd_alloc_info();
  1478. if (!reply)
  1479. return -ENOMEM;
  1480. ovs_lock();
  1481. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1482. if (IS_ERR(dp)) {
  1483. err = PTR_ERR(dp);
  1484. goto err_unlock_free;
  1485. }
  1486. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1487. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1488. BUG_ON(err < 0);
  1489. ovs_unlock();
  1490. return genlmsg_reply(reply, info);
  1491. err_unlock_free:
  1492. ovs_unlock();
  1493. kfree_skb(reply);
  1494. return err;
  1495. }
  1496. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1497. {
  1498. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1499. struct datapath *dp;
  1500. int skip = cb->args[0];
  1501. int i = 0;
  1502. ovs_lock();
  1503. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1504. if (i >= skip &&
  1505. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1506. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1507. OVS_DP_CMD_NEW) < 0)
  1508. break;
  1509. i++;
  1510. }
  1511. ovs_unlock();
  1512. cb->args[0] = i;
  1513. return skb->len;
  1514. }
  1515. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1516. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1517. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1518. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1519. };
  1520. static const struct genl_ops dp_datapath_genl_ops[] = {
  1521. { .cmd = OVS_DP_CMD_NEW,
  1522. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1523. .policy = datapath_policy,
  1524. .doit = ovs_dp_cmd_new
  1525. },
  1526. { .cmd = OVS_DP_CMD_DEL,
  1527. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1528. .policy = datapath_policy,
  1529. .doit = ovs_dp_cmd_del
  1530. },
  1531. { .cmd = OVS_DP_CMD_GET,
  1532. .flags = 0, /* OK for unprivileged users. */
  1533. .policy = datapath_policy,
  1534. .doit = ovs_dp_cmd_get,
  1535. .dumpit = ovs_dp_cmd_dump
  1536. },
  1537. { .cmd = OVS_DP_CMD_SET,
  1538. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1539. .policy = datapath_policy,
  1540. .doit = ovs_dp_cmd_set,
  1541. },
  1542. };
  1543. static struct genl_family dp_datapath_genl_family __ro_after_init = {
  1544. .hdrsize = sizeof(struct ovs_header),
  1545. .name = OVS_DATAPATH_FAMILY,
  1546. .version = OVS_DATAPATH_VERSION,
  1547. .maxattr = OVS_DP_ATTR_MAX,
  1548. .netnsok = true,
  1549. .parallel_ops = true,
  1550. .ops = dp_datapath_genl_ops,
  1551. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1552. .mcgrps = &ovs_dp_datapath_multicast_group,
  1553. .n_mcgrps = 1,
  1554. .module = THIS_MODULE,
  1555. };
  1556. /* Called with ovs_mutex or RCU read lock. */
  1557. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1558. u32 portid, u32 seq, u32 flags, u8 cmd)
  1559. {
  1560. struct ovs_header *ovs_header;
  1561. struct ovs_vport_stats vport_stats;
  1562. int err;
  1563. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1564. flags, cmd);
  1565. if (!ovs_header)
  1566. return -EMSGSIZE;
  1567. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1568. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1569. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1570. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1571. ovs_vport_name(vport)))
  1572. goto nla_put_failure;
  1573. ovs_vport_get_stats(vport, &vport_stats);
  1574. if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
  1575. sizeof(struct ovs_vport_stats), &vport_stats,
  1576. OVS_VPORT_ATTR_PAD))
  1577. goto nla_put_failure;
  1578. if (ovs_vport_get_upcall_portids(vport, skb))
  1579. goto nla_put_failure;
  1580. err = ovs_vport_get_options(vport, skb);
  1581. if (err == -EMSGSIZE)
  1582. goto error;
  1583. genlmsg_end(skb, ovs_header);
  1584. return 0;
  1585. nla_put_failure:
  1586. err = -EMSGSIZE;
  1587. error:
  1588. genlmsg_cancel(skb, ovs_header);
  1589. return err;
  1590. }
  1591. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1592. {
  1593. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1594. }
  1595. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1596. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1597. u32 seq, u8 cmd)
  1598. {
  1599. struct sk_buff *skb;
  1600. int retval;
  1601. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1602. if (!skb)
  1603. return ERR_PTR(-ENOMEM);
  1604. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1605. BUG_ON(retval < 0);
  1606. return skb;
  1607. }
  1608. /* Called with ovs_mutex or RCU read lock. */
  1609. static struct vport *lookup_vport(struct net *net,
  1610. const struct ovs_header *ovs_header,
  1611. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1612. {
  1613. struct datapath *dp;
  1614. struct vport *vport;
  1615. if (a[OVS_VPORT_ATTR_NAME]) {
  1616. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1617. if (!vport)
  1618. return ERR_PTR(-ENODEV);
  1619. if (ovs_header->dp_ifindex &&
  1620. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1621. return ERR_PTR(-ENODEV);
  1622. return vport;
  1623. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1624. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1625. if (port_no >= DP_MAX_PORTS)
  1626. return ERR_PTR(-EFBIG);
  1627. dp = get_dp(net, ovs_header->dp_ifindex);
  1628. if (!dp)
  1629. return ERR_PTR(-ENODEV);
  1630. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1631. if (!vport)
  1632. return ERR_PTR(-ENODEV);
  1633. return vport;
  1634. } else
  1635. return ERR_PTR(-EINVAL);
  1636. }
  1637. /* Called with ovs_mutex */
  1638. static void update_headroom(struct datapath *dp)
  1639. {
  1640. unsigned dev_headroom, max_headroom = 0;
  1641. struct net_device *dev;
  1642. struct vport *vport;
  1643. int i;
  1644. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1645. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1646. dev = vport->dev;
  1647. dev_headroom = netdev_get_fwd_headroom(dev);
  1648. if (dev_headroom > max_headroom)
  1649. max_headroom = dev_headroom;
  1650. }
  1651. }
  1652. dp->max_headroom = max_headroom;
  1653. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1654. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
  1655. netdev_set_rx_headroom(vport->dev, max_headroom);
  1656. }
  1657. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1658. {
  1659. struct nlattr **a = info->attrs;
  1660. struct ovs_header *ovs_header = info->userhdr;
  1661. struct vport_parms parms;
  1662. struct sk_buff *reply;
  1663. struct vport *vport;
  1664. struct datapath *dp;
  1665. u32 port_no;
  1666. int err;
  1667. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1668. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1669. return -EINVAL;
  1670. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1671. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1672. if (port_no >= DP_MAX_PORTS)
  1673. return -EFBIG;
  1674. reply = ovs_vport_cmd_alloc_info();
  1675. if (!reply)
  1676. return -ENOMEM;
  1677. ovs_lock();
  1678. restart:
  1679. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1680. err = -ENODEV;
  1681. if (!dp)
  1682. goto exit_unlock_free;
  1683. if (port_no) {
  1684. vport = ovs_vport_ovsl(dp, port_no);
  1685. err = -EBUSY;
  1686. if (vport)
  1687. goto exit_unlock_free;
  1688. } else {
  1689. for (port_no = 1; ; port_no++) {
  1690. if (port_no >= DP_MAX_PORTS) {
  1691. err = -EFBIG;
  1692. goto exit_unlock_free;
  1693. }
  1694. vport = ovs_vport_ovsl(dp, port_no);
  1695. if (!vport)
  1696. break;
  1697. }
  1698. }
  1699. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1700. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1701. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1702. parms.dp = dp;
  1703. parms.port_no = port_no;
  1704. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1705. vport = new_vport(&parms);
  1706. err = PTR_ERR(vport);
  1707. if (IS_ERR(vport)) {
  1708. if (err == -EAGAIN)
  1709. goto restart;
  1710. goto exit_unlock_free;
  1711. }
  1712. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1713. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1714. if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
  1715. update_headroom(dp);
  1716. else
  1717. netdev_set_rx_headroom(vport->dev, dp->max_headroom);
  1718. BUG_ON(err < 0);
  1719. ovs_unlock();
  1720. ovs_notify(&dp_vport_genl_family, reply, info);
  1721. return 0;
  1722. exit_unlock_free:
  1723. ovs_unlock();
  1724. kfree_skb(reply);
  1725. return err;
  1726. }
  1727. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1728. {
  1729. struct nlattr **a = info->attrs;
  1730. struct sk_buff *reply;
  1731. struct vport *vport;
  1732. int err;
  1733. reply = ovs_vport_cmd_alloc_info();
  1734. if (!reply)
  1735. return -ENOMEM;
  1736. ovs_lock();
  1737. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1738. err = PTR_ERR(vport);
  1739. if (IS_ERR(vport))
  1740. goto exit_unlock_free;
  1741. if (a[OVS_VPORT_ATTR_TYPE] &&
  1742. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1743. err = -EINVAL;
  1744. goto exit_unlock_free;
  1745. }
  1746. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1747. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1748. if (err)
  1749. goto exit_unlock_free;
  1750. }
  1751. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1752. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1753. err = ovs_vport_set_upcall_portids(vport, ids);
  1754. if (err)
  1755. goto exit_unlock_free;
  1756. }
  1757. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1758. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1759. BUG_ON(err < 0);
  1760. ovs_unlock();
  1761. ovs_notify(&dp_vport_genl_family, reply, info);
  1762. return 0;
  1763. exit_unlock_free:
  1764. ovs_unlock();
  1765. kfree_skb(reply);
  1766. return err;
  1767. }
  1768. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1769. {
  1770. bool must_update_headroom = false;
  1771. struct nlattr **a = info->attrs;
  1772. struct sk_buff *reply;
  1773. struct datapath *dp;
  1774. struct vport *vport;
  1775. int err;
  1776. reply = ovs_vport_cmd_alloc_info();
  1777. if (!reply)
  1778. return -ENOMEM;
  1779. ovs_lock();
  1780. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1781. err = PTR_ERR(vport);
  1782. if (IS_ERR(vport))
  1783. goto exit_unlock_free;
  1784. if (vport->port_no == OVSP_LOCAL) {
  1785. err = -EINVAL;
  1786. goto exit_unlock_free;
  1787. }
  1788. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1789. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1790. BUG_ON(err < 0);
  1791. /* the vport deletion may trigger dp headroom update */
  1792. dp = vport->dp;
  1793. if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
  1794. must_update_headroom = true;
  1795. netdev_reset_rx_headroom(vport->dev);
  1796. ovs_dp_detach_port(vport);
  1797. if (must_update_headroom)
  1798. update_headroom(dp);
  1799. ovs_unlock();
  1800. ovs_notify(&dp_vport_genl_family, reply, info);
  1801. return 0;
  1802. exit_unlock_free:
  1803. ovs_unlock();
  1804. kfree_skb(reply);
  1805. return err;
  1806. }
  1807. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1808. {
  1809. struct nlattr **a = info->attrs;
  1810. struct ovs_header *ovs_header = info->userhdr;
  1811. struct sk_buff *reply;
  1812. struct vport *vport;
  1813. int err;
  1814. reply = ovs_vport_cmd_alloc_info();
  1815. if (!reply)
  1816. return -ENOMEM;
  1817. rcu_read_lock();
  1818. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1819. err = PTR_ERR(vport);
  1820. if (IS_ERR(vport))
  1821. goto exit_unlock_free;
  1822. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1823. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1824. BUG_ON(err < 0);
  1825. rcu_read_unlock();
  1826. return genlmsg_reply(reply, info);
  1827. exit_unlock_free:
  1828. rcu_read_unlock();
  1829. kfree_skb(reply);
  1830. return err;
  1831. }
  1832. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1833. {
  1834. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1835. struct datapath *dp;
  1836. int bucket = cb->args[0], skip = cb->args[1];
  1837. int i, j = 0;
  1838. rcu_read_lock();
  1839. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1840. if (!dp) {
  1841. rcu_read_unlock();
  1842. return -ENODEV;
  1843. }
  1844. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1845. struct vport *vport;
  1846. j = 0;
  1847. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1848. if (j >= skip &&
  1849. ovs_vport_cmd_fill_info(vport, skb,
  1850. NETLINK_CB(cb->skb).portid,
  1851. cb->nlh->nlmsg_seq,
  1852. NLM_F_MULTI,
  1853. OVS_VPORT_CMD_NEW) < 0)
  1854. goto out;
  1855. j++;
  1856. }
  1857. skip = 0;
  1858. }
  1859. out:
  1860. rcu_read_unlock();
  1861. cb->args[0] = i;
  1862. cb->args[1] = j;
  1863. return skb->len;
  1864. }
  1865. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1866. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1867. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1868. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1869. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1870. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1871. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1872. };
  1873. static const struct genl_ops dp_vport_genl_ops[] = {
  1874. { .cmd = OVS_VPORT_CMD_NEW,
  1875. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1876. .policy = vport_policy,
  1877. .doit = ovs_vport_cmd_new
  1878. },
  1879. { .cmd = OVS_VPORT_CMD_DEL,
  1880. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1881. .policy = vport_policy,
  1882. .doit = ovs_vport_cmd_del
  1883. },
  1884. { .cmd = OVS_VPORT_CMD_GET,
  1885. .flags = 0, /* OK for unprivileged users. */
  1886. .policy = vport_policy,
  1887. .doit = ovs_vport_cmd_get,
  1888. .dumpit = ovs_vport_cmd_dump
  1889. },
  1890. { .cmd = OVS_VPORT_CMD_SET,
  1891. .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1892. .policy = vport_policy,
  1893. .doit = ovs_vport_cmd_set,
  1894. },
  1895. };
  1896. struct genl_family dp_vport_genl_family __ro_after_init = {
  1897. .hdrsize = sizeof(struct ovs_header),
  1898. .name = OVS_VPORT_FAMILY,
  1899. .version = OVS_VPORT_VERSION,
  1900. .maxattr = OVS_VPORT_ATTR_MAX,
  1901. .netnsok = true,
  1902. .parallel_ops = true,
  1903. .ops = dp_vport_genl_ops,
  1904. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1905. .mcgrps = &ovs_dp_vport_multicast_group,
  1906. .n_mcgrps = 1,
  1907. .module = THIS_MODULE,
  1908. };
  1909. static struct genl_family * const dp_genl_families[] = {
  1910. &dp_datapath_genl_family,
  1911. &dp_vport_genl_family,
  1912. &dp_flow_genl_family,
  1913. &dp_packet_genl_family,
  1914. };
  1915. static void dp_unregister_genl(int n_families)
  1916. {
  1917. int i;
  1918. for (i = 0; i < n_families; i++)
  1919. genl_unregister_family(dp_genl_families[i]);
  1920. }
  1921. static int __init dp_register_genl(void)
  1922. {
  1923. int err;
  1924. int i;
  1925. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1926. err = genl_register_family(dp_genl_families[i]);
  1927. if (err)
  1928. goto error;
  1929. }
  1930. return 0;
  1931. error:
  1932. dp_unregister_genl(i);
  1933. return err;
  1934. }
  1935. static int __net_init ovs_init_net(struct net *net)
  1936. {
  1937. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1938. INIT_LIST_HEAD(&ovs_net->dps);
  1939. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1940. ovs_ct_init(net);
  1941. return 0;
  1942. }
  1943. static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
  1944. struct list_head *head)
  1945. {
  1946. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1947. struct datapath *dp;
  1948. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1949. int i;
  1950. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1951. struct vport *vport;
  1952. hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
  1953. if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
  1954. continue;
  1955. if (dev_net(vport->dev) == dnet)
  1956. list_add(&vport->detach_list, head);
  1957. }
  1958. }
  1959. }
  1960. }
  1961. static void __net_exit ovs_exit_net(struct net *dnet)
  1962. {
  1963. struct datapath *dp, *dp_next;
  1964. struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
  1965. struct vport *vport, *vport_next;
  1966. struct net *net;
  1967. LIST_HEAD(head);
  1968. ovs_ct_exit(dnet);
  1969. ovs_lock();
  1970. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1971. __dp_destroy(dp);
  1972. rtnl_lock();
  1973. for_each_net(net)
  1974. list_vports_from_net(net, dnet, &head);
  1975. rtnl_unlock();
  1976. /* Detach all vports from given namespace. */
  1977. list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
  1978. list_del(&vport->detach_list);
  1979. ovs_dp_detach_port(vport);
  1980. }
  1981. ovs_unlock();
  1982. cancel_work_sync(&ovs_net->dp_notify_work);
  1983. }
  1984. static struct pernet_operations ovs_net_ops = {
  1985. .init = ovs_init_net,
  1986. .exit = ovs_exit_net,
  1987. .id = &ovs_net_id,
  1988. .size = sizeof(struct ovs_net),
  1989. };
  1990. static int __init dp_init(void)
  1991. {
  1992. int err;
  1993. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1994. pr_info("Open vSwitch switching datapath\n");
  1995. err = action_fifos_init();
  1996. if (err)
  1997. goto error;
  1998. err = ovs_internal_dev_rtnl_link_register();
  1999. if (err)
  2000. goto error_action_fifos_exit;
  2001. err = ovs_flow_init();
  2002. if (err)
  2003. goto error_unreg_rtnl_link;
  2004. err = ovs_vport_init();
  2005. if (err)
  2006. goto error_flow_exit;
  2007. err = register_pernet_device(&ovs_net_ops);
  2008. if (err)
  2009. goto error_vport_exit;
  2010. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  2011. if (err)
  2012. goto error_netns_exit;
  2013. err = ovs_netdev_init();
  2014. if (err)
  2015. goto error_unreg_notifier;
  2016. err = dp_register_genl();
  2017. if (err < 0)
  2018. goto error_unreg_netdev;
  2019. return 0;
  2020. error_unreg_netdev:
  2021. ovs_netdev_exit();
  2022. error_unreg_notifier:
  2023. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2024. error_netns_exit:
  2025. unregister_pernet_device(&ovs_net_ops);
  2026. error_vport_exit:
  2027. ovs_vport_exit();
  2028. error_flow_exit:
  2029. ovs_flow_exit();
  2030. error_unreg_rtnl_link:
  2031. ovs_internal_dev_rtnl_link_unregister();
  2032. error_action_fifos_exit:
  2033. action_fifos_exit();
  2034. error:
  2035. return err;
  2036. }
  2037. static void dp_cleanup(void)
  2038. {
  2039. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  2040. ovs_netdev_exit();
  2041. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  2042. unregister_pernet_device(&ovs_net_ops);
  2043. rcu_barrier();
  2044. ovs_vport_exit();
  2045. ovs_flow_exit();
  2046. ovs_internal_dev_rtnl_link_unregister();
  2047. action_fifos_exit();
  2048. }
  2049. module_init(dp_init);
  2050. module_exit(dp_cleanup);
  2051. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  2052. MODULE_LICENSE("GPL");
  2053. MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
  2054. MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
  2055. MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
  2056. MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);