datapath.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  1. /*
  2. * Copyright (c) 2007-2012 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);
  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. int key_len;
  196. stats = this_cpu_ptr(dp->stats_percpu);
  197. /* Extract flow from 'skb' into 'key'. */
  198. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  199. if (unlikely(error)) {
  200. kfree_skb(skb);
  201. return;
  202. }
  203. /* Look up flow. */
  204. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  205. if (unlikely(!flow)) {
  206. struct dp_upcall_info upcall;
  207. upcall.cmd = OVS_PACKET_CMD_MISS;
  208. upcall.key = &key;
  209. upcall.userdata = NULL;
  210. upcall.portid = p->upcall_portid;
  211. ovs_dp_upcall(dp, skb, &upcall);
  212. consume_skb(skb);
  213. stats_counter = &stats->n_missed;
  214. goto out;
  215. }
  216. OVS_CB(skb)->flow = flow;
  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(4) /* OVS_KEY_ATTR_IN_PORT */
  309. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  310. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  311. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  312. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  313. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  314. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  315. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  316. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  317. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  318. }
  319. static size_t upcall_msg_size(const struct sk_buff *skb,
  320. const struct nlattr *userdata)
  321. {
  322. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  323. + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
  324. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  325. /* OVS_PACKET_ATTR_USERDATA */
  326. if (userdata)
  327. size += NLA_ALIGN(userdata->nla_len);
  328. return size;
  329. }
  330. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  331. struct sk_buff *skb,
  332. const struct dp_upcall_info *upcall_info)
  333. {
  334. struct ovs_header *upcall;
  335. struct sk_buff *nskb = NULL;
  336. struct sk_buff *user_skb; /* to be queued to userspace */
  337. struct nlattr *nla;
  338. int err;
  339. if (vlan_tx_tag_present(skb)) {
  340. nskb = skb_clone(skb, GFP_ATOMIC);
  341. if (!nskb)
  342. return -ENOMEM;
  343. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  344. if (!nskb)
  345. return -ENOMEM;
  346. nskb->vlan_tci = 0;
  347. skb = nskb;
  348. }
  349. if (nla_attr_size(skb->len) > USHRT_MAX) {
  350. err = -EFBIG;
  351. goto out;
  352. }
  353. user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
  354. if (!user_skb) {
  355. err = -ENOMEM;
  356. goto out;
  357. }
  358. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  359. 0, upcall_info->cmd);
  360. upcall->dp_ifindex = dp_ifindex;
  361. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  362. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  363. nla_nest_end(user_skb, nla);
  364. if (upcall_info->userdata)
  365. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  366. nla_len(upcall_info->userdata),
  367. nla_data(upcall_info->userdata));
  368. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  369. skb_copy_and_csum_dev(skb, nla_data(nla));
  370. genlmsg_end(user_skb, upcall);
  371. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  372. out:
  373. kfree_skb(nskb);
  374. return err;
  375. }
  376. /* Called with ovs_mutex. */
  377. static int flush_flows(struct datapath *dp)
  378. {
  379. struct flow_table *old_table;
  380. struct flow_table *new_table;
  381. old_table = ovsl_dereference(dp->table);
  382. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  383. if (!new_table)
  384. return -ENOMEM;
  385. rcu_assign_pointer(dp->table, new_table);
  386. ovs_flow_tbl_deferred_destroy(old_table);
  387. return 0;
  388. }
  389. static int validate_actions(const struct nlattr *attr,
  390. const struct sw_flow_key *key, int depth);
  391. static int validate_sample(const struct nlattr *attr,
  392. const struct sw_flow_key *key, int depth)
  393. {
  394. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  395. const struct nlattr *probability, *actions;
  396. const struct nlattr *a;
  397. int rem;
  398. memset(attrs, 0, sizeof(attrs));
  399. nla_for_each_nested(a, attr, rem) {
  400. int type = nla_type(a);
  401. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  402. return -EINVAL;
  403. attrs[type] = a;
  404. }
  405. if (rem)
  406. return -EINVAL;
  407. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  408. if (!probability || nla_len(probability) != sizeof(u32))
  409. return -EINVAL;
  410. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  411. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  412. return -EINVAL;
  413. return validate_actions(actions, key, depth + 1);
  414. }
  415. static int validate_tp_port(const struct sw_flow_key *flow_key)
  416. {
  417. if (flow_key->eth.type == htons(ETH_P_IP)) {
  418. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  419. return 0;
  420. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  421. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  422. return 0;
  423. }
  424. return -EINVAL;
  425. }
  426. static int validate_set(const struct nlattr *a,
  427. const struct sw_flow_key *flow_key)
  428. {
  429. const struct nlattr *ovs_key = nla_data(a);
  430. int key_type = nla_type(ovs_key);
  431. /* There can be only one key in a action */
  432. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  433. return -EINVAL;
  434. if (key_type > OVS_KEY_ATTR_MAX ||
  435. nla_len(ovs_key) != ovs_key_lens[key_type])
  436. return -EINVAL;
  437. switch (key_type) {
  438. const struct ovs_key_ipv4 *ipv4_key;
  439. const struct ovs_key_ipv6 *ipv6_key;
  440. case OVS_KEY_ATTR_PRIORITY:
  441. case OVS_KEY_ATTR_SKB_MARK:
  442. case OVS_KEY_ATTR_ETHERNET:
  443. break;
  444. case OVS_KEY_ATTR_IPV4:
  445. if (flow_key->eth.type != htons(ETH_P_IP))
  446. return -EINVAL;
  447. if (!flow_key->ip.proto)
  448. return -EINVAL;
  449. ipv4_key = nla_data(ovs_key);
  450. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  451. return -EINVAL;
  452. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  453. return -EINVAL;
  454. break;
  455. case OVS_KEY_ATTR_IPV6:
  456. if (flow_key->eth.type != htons(ETH_P_IPV6))
  457. return -EINVAL;
  458. if (!flow_key->ip.proto)
  459. return -EINVAL;
  460. ipv6_key = nla_data(ovs_key);
  461. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  462. return -EINVAL;
  463. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  464. return -EINVAL;
  465. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  466. return -EINVAL;
  467. break;
  468. case OVS_KEY_ATTR_TCP:
  469. if (flow_key->ip.proto != IPPROTO_TCP)
  470. return -EINVAL;
  471. return validate_tp_port(flow_key);
  472. case OVS_KEY_ATTR_UDP:
  473. if (flow_key->ip.proto != IPPROTO_UDP)
  474. return -EINVAL;
  475. return validate_tp_port(flow_key);
  476. default:
  477. return -EINVAL;
  478. }
  479. return 0;
  480. }
  481. static int validate_userspace(const struct nlattr *attr)
  482. {
  483. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  484. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  485. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
  486. };
  487. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  488. int error;
  489. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  490. attr, userspace_policy);
  491. if (error)
  492. return error;
  493. if (!a[OVS_USERSPACE_ATTR_PID] ||
  494. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  495. return -EINVAL;
  496. return 0;
  497. }
  498. static int validate_actions(const struct nlattr *attr,
  499. const struct sw_flow_key *key, int depth)
  500. {
  501. const struct nlattr *a;
  502. int rem, err;
  503. if (depth >= SAMPLE_ACTION_DEPTH)
  504. return -EOVERFLOW;
  505. nla_for_each_nested(a, attr, rem) {
  506. /* Expected argument lengths, (u32)-1 for variable length. */
  507. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  508. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  509. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  510. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  511. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  512. [OVS_ACTION_ATTR_SET] = (u32)-1,
  513. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  514. };
  515. const struct ovs_action_push_vlan *vlan;
  516. int type = nla_type(a);
  517. if (type > OVS_ACTION_ATTR_MAX ||
  518. (action_lens[type] != nla_len(a) &&
  519. action_lens[type] != (u32)-1))
  520. return -EINVAL;
  521. switch (type) {
  522. case OVS_ACTION_ATTR_UNSPEC:
  523. return -EINVAL;
  524. case OVS_ACTION_ATTR_USERSPACE:
  525. err = validate_userspace(a);
  526. if (err)
  527. return err;
  528. break;
  529. case OVS_ACTION_ATTR_OUTPUT:
  530. if (nla_get_u32(a) >= DP_MAX_PORTS)
  531. return -EINVAL;
  532. break;
  533. case OVS_ACTION_ATTR_POP_VLAN:
  534. break;
  535. case OVS_ACTION_ATTR_PUSH_VLAN:
  536. vlan = nla_data(a);
  537. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  538. return -EINVAL;
  539. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  540. return -EINVAL;
  541. break;
  542. case OVS_ACTION_ATTR_SET:
  543. err = validate_set(a, key);
  544. if (err)
  545. return err;
  546. break;
  547. case OVS_ACTION_ATTR_SAMPLE:
  548. err = validate_sample(a, key, depth);
  549. if (err)
  550. return err;
  551. break;
  552. default:
  553. return -EINVAL;
  554. }
  555. }
  556. if (rem > 0)
  557. return -EINVAL;
  558. return 0;
  559. }
  560. static void clear_stats(struct sw_flow *flow)
  561. {
  562. flow->used = 0;
  563. flow->tcp_flags = 0;
  564. flow->packet_count = 0;
  565. flow->byte_count = 0;
  566. }
  567. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  568. {
  569. struct ovs_header *ovs_header = info->userhdr;
  570. struct nlattr **a = info->attrs;
  571. struct sw_flow_actions *acts;
  572. struct sk_buff *packet;
  573. struct sw_flow *flow;
  574. struct datapath *dp;
  575. struct ethhdr *eth;
  576. int len;
  577. int err;
  578. int key_len;
  579. err = -EINVAL;
  580. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  581. !a[OVS_PACKET_ATTR_ACTIONS])
  582. goto err;
  583. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  584. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  585. err = -ENOMEM;
  586. if (!packet)
  587. goto err;
  588. skb_reserve(packet, NET_IP_ALIGN);
  589. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  590. skb_reset_mac_header(packet);
  591. eth = eth_hdr(packet);
  592. /* Normally, setting the skb 'protocol' field would be handled by a
  593. * call to eth_type_trans(), but it assumes there's a sending
  594. * device, which we may not have. */
  595. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  596. packet->protocol = eth->h_proto;
  597. else
  598. packet->protocol = htons(ETH_P_802_2);
  599. /* Build an sw_flow for sending this packet. */
  600. flow = ovs_flow_alloc();
  601. err = PTR_ERR(flow);
  602. if (IS_ERR(flow))
  603. goto err_kfree_skb;
  604. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  605. if (err)
  606. goto err_flow_free;
  607. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  608. &flow->key.phy.skb_mark,
  609. &flow->key.phy.in_port,
  610. a[OVS_PACKET_ATTR_KEY]);
  611. if (err)
  612. goto err_flow_free;
  613. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  614. if (err)
  615. goto err_flow_free;
  616. flow->hash = ovs_flow_hash(&flow->key, key_len);
  617. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  618. err = PTR_ERR(acts);
  619. if (IS_ERR(acts))
  620. goto err_flow_free;
  621. rcu_assign_pointer(flow->sf_acts, acts);
  622. OVS_CB(packet)->flow = flow;
  623. packet->priority = flow->key.phy.priority;
  624. packet->mark = flow->key.phy.skb_mark;
  625. rcu_read_lock();
  626. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  627. err = -ENODEV;
  628. if (!dp)
  629. goto err_unlock;
  630. local_bh_disable();
  631. err = ovs_execute_actions(dp, packet);
  632. local_bh_enable();
  633. rcu_read_unlock();
  634. ovs_flow_free(flow);
  635. return err;
  636. err_unlock:
  637. rcu_read_unlock();
  638. err_flow_free:
  639. ovs_flow_free(flow);
  640. err_kfree_skb:
  641. kfree_skb(packet);
  642. err:
  643. return err;
  644. }
  645. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  646. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  647. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  648. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  649. };
  650. static struct genl_ops dp_packet_genl_ops[] = {
  651. { .cmd = OVS_PACKET_CMD_EXECUTE,
  652. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  653. .policy = packet_policy,
  654. .doit = ovs_packet_cmd_execute
  655. }
  656. };
  657. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  658. {
  659. int i;
  660. struct flow_table *table = ovsl_dereference(dp->table);
  661. stats->n_flows = ovs_flow_tbl_count(table);
  662. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  663. for_each_possible_cpu(i) {
  664. const struct dp_stats_percpu *percpu_stats;
  665. struct dp_stats_percpu local_stats;
  666. unsigned int start;
  667. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  668. do {
  669. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  670. local_stats = *percpu_stats;
  671. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  672. stats->n_hit += local_stats.n_hit;
  673. stats->n_missed += local_stats.n_missed;
  674. stats->n_lost += local_stats.n_lost;
  675. }
  676. }
  677. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  678. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  679. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  680. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  681. };
  682. static struct genl_family dp_flow_genl_family = {
  683. .id = GENL_ID_GENERATE,
  684. .hdrsize = sizeof(struct ovs_header),
  685. .name = OVS_FLOW_FAMILY,
  686. .version = OVS_FLOW_VERSION,
  687. .maxattr = OVS_FLOW_ATTR_MAX,
  688. .netnsok = true,
  689. .parallel_ops = true,
  690. };
  691. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  692. .name = OVS_FLOW_MCGROUP
  693. };
  694. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  695. {
  696. return NLMSG_ALIGN(sizeof(struct ovs_header))
  697. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  698. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  699. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  700. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  701. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  702. }
  703. /* Called with ovs_mutex. */
  704. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  705. struct sk_buff *skb, u32 portid,
  706. u32 seq, u32 flags, u8 cmd)
  707. {
  708. const int skb_orig_len = skb->len;
  709. const struct sw_flow_actions *sf_acts;
  710. struct ovs_flow_stats stats;
  711. struct ovs_header *ovs_header;
  712. struct nlattr *nla;
  713. unsigned long used;
  714. u8 tcp_flags;
  715. int err;
  716. sf_acts = ovsl_dereference(flow->sf_acts);
  717. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  718. if (!ovs_header)
  719. return -EMSGSIZE;
  720. ovs_header->dp_ifindex = get_dpifindex(dp);
  721. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  722. if (!nla)
  723. goto nla_put_failure;
  724. err = ovs_flow_to_nlattrs(&flow->key, skb);
  725. if (err)
  726. goto error;
  727. nla_nest_end(skb, nla);
  728. spin_lock_bh(&flow->lock);
  729. used = flow->used;
  730. stats.n_packets = flow->packet_count;
  731. stats.n_bytes = flow->byte_count;
  732. tcp_flags = flow->tcp_flags;
  733. spin_unlock_bh(&flow->lock);
  734. if (used &&
  735. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  736. goto nla_put_failure;
  737. if (stats.n_packets &&
  738. nla_put(skb, OVS_FLOW_ATTR_STATS,
  739. sizeof(struct ovs_flow_stats), &stats))
  740. goto nla_put_failure;
  741. if (tcp_flags &&
  742. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  743. goto nla_put_failure;
  744. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  745. * this is the first flow to be dumped into 'skb'. This is unusual for
  746. * Netlink but individual action lists can be longer than
  747. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  748. * The userspace caller can always fetch the actions separately if it
  749. * really wants them. (Most userspace callers in fact don't care.)
  750. *
  751. * This can only fail for dump operations because the skb is always
  752. * properly sized for single flows.
  753. */
  754. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  755. sf_acts->actions);
  756. if (err < 0 && skb_orig_len)
  757. goto error;
  758. return genlmsg_end(skb, ovs_header);
  759. nla_put_failure:
  760. err = -EMSGSIZE;
  761. error:
  762. genlmsg_cancel(skb, ovs_header);
  763. return err;
  764. }
  765. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  766. {
  767. const struct sw_flow_actions *sf_acts;
  768. sf_acts = ovsl_dereference(flow->sf_acts);
  769. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  770. }
  771. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  772. struct datapath *dp,
  773. u32 portid, u32 seq, u8 cmd)
  774. {
  775. struct sk_buff *skb;
  776. int retval;
  777. skb = ovs_flow_cmd_alloc_info(flow);
  778. if (!skb)
  779. return ERR_PTR(-ENOMEM);
  780. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  781. BUG_ON(retval < 0);
  782. return skb;
  783. }
  784. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  785. {
  786. struct nlattr **a = info->attrs;
  787. struct ovs_header *ovs_header = info->userhdr;
  788. struct sw_flow_key key;
  789. struct sw_flow *flow;
  790. struct sk_buff *reply;
  791. struct datapath *dp;
  792. struct flow_table *table;
  793. int error;
  794. int key_len;
  795. /* Extract key. */
  796. error = -EINVAL;
  797. if (!a[OVS_FLOW_ATTR_KEY])
  798. goto error;
  799. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  800. if (error)
  801. goto error;
  802. /* Validate actions. */
  803. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  804. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  805. if (error)
  806. goto error;
  807. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  808. error = -EINVAL;
  809. goto error;
  810. }
  811. ovs_lock();
  812. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  813. error = -ENODEV;
  814. if (!dp)
  815. goto err_unlock_ovs;
  816. table = ovsl_dereference(dp->table);
  817. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  818. if (!flow) {
  819. struct sw_flow_actions *acts;
  820. /* Bail out if we're not allowed to create a new flow. */
  821. error = -ENOENT;
  822. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  823. goto err_unlock_ovs;
  824. /* Expand table, if necessary, to make room. */
  825. if (ovs_flow_tbl_need_to_expand(table)) {
  826. struct flow_table *new_table;
  827. new_table = ovs_flow_tbl_expand(table);
  828. if (!IS_ERR(new_table)) {
  829. rcu_assign_pointer(dp->table, new_table);
  830. ovs_flow_tbl_deferred_destroy(table);
  831. table = ovsl_dereference(dp->table);
  832. }
  833. }
  834. /* Allocate flow. */
  835. flow = ovs_flow_alloc();
  836. if (IS_ERR(flow)) {
  837. error = PTR_ERR(flow);
  838. goto err_unlock_ovs;
  839. }
  840. flow->key = key;
  841. clear_stats(flow);
  842. /* Obtain actions. */
  843. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  844. error = PTR_ERR(acts);
  845. if (IS_ERR(acts))
  846. goto error_free_flow;
  847. rcu_assign_pointer(flow->sf_acts, acts);
  848. /* Put flow in bucket. */
  849. flow->hash = ovs_flow_hash(&key, key_len);
  850. ovs_flow_tbl_insert(table, flow);
  851. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  852. info->snd_seq,
  853. OVS_FLOW_CMD_NEW);
  854. } else {
  855. /* We found a matching flow. */
  856. struct sw_flow_actions *old_acts;
  857. struct nlattr *acts_attrs;
  858. /* Bail out if we're not allowed to modify an existing flow.
  859. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  860. * because Generic Netlink treats the latter as a dump
  861. * request. We also accept NLM_F_EXCL in case that bug ever
  862. * gets fixed.
  863. */
  864. error = -EEXIST;
  865. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  866. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  867. goto err_unlock_ovs;
  868. /* Update actions. */
  869. old_acts = ovsl_dereference(flow->sf_acts);
  870. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  871. if (acts_attrs &&
  872. (old_acts->actions_len != nla_len(acts_attrs) ||
  873. memcmp(old_acts->actions, nla_data(acts_attrs),
  874. old_acts->actions_len))) {
  875. struct sw_flow_actions *new_acts;
  876. new_acts = ovs_flow_actions_alloc(acts_attrs);
  877. error = PTR_ERR(new_acts);
  878. if (IS_ERR(new_acts))
  879. goto err_unlock_ovs;
  880. rcu_assign_pointer(flow->sf_acts, new_acts);
  881. ovs_flow_deferred_free_acts(old_acts);
  882. }
  883. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  884. info->snd_seq, OVS_FLOW_CMD_NEW);
  885. /* Clear stats. */
  886. if (a[OVS_FLOW_ATTR_CLEAR]) {
  887. spin_lock_bh(&flow->lock);
  888. clear_stats(flow);
  889. spin_unlock_bh(&flow->lock);
  890. }
  891. }
  892. ovs_unlock();
  893. if (!IS_ERR(reply))
  894. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  895. else
  896. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  897. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  898. return 0;
  899. error_free_flow:
  900. ovs_flow_free(flow);
  901. err_unlock_ovs:
  902. ovs_unlock();
  903. error:
  904. return error;
  905. }
  906. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  907. {
  908. struct nlattr **a = info->attrs;
  909. struct ovs_header *ovs_header = info->userhdr;
  910. struct sw_flow_key key;
  911. struct sk_buff *reply;
  912. struct sw_flow *flow;
  913. struct datapath *dp;
  914. struct flow_table *table;
  915. int err;
  916. int key_len;
  917. if (!a[OVS_FLOW_ATTR_KEY])
  918. return -EINVAL;
  919. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  920. if (err)
  921. return err;
  922. ovs_lock();
  923. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  924. if (!dp) {
  925. err = -ENODEV;
  926. goto unlock;
  927. }
  928. table = ovsl_dereference(dp->table);
  929. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  930. if (!flow) {
  931. err = -ENOENT;
  932. goto unlock;
  933. }
  934. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  935. info->snd_seq, OVS_FLOW_CMD_NEW);
  936. if (IS_ERR(reply)) {
  937. err = PTR_ERR(reply);
  938. goto unlock;
  939. }
  940. ovs_unlock();
  941. return genlmsg_reply(reply, info);
  942. unlock:
  943. ovs_unlock();
  944. return err;
  945. }
  946. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  947. {
  948. struct nlattr **a = info->attrs;
  949. struct ovs_header *ovs_header = info->userhdr;
  950. struct sw_flow_key key;
  951. struct sk_buff *reply;
  952. struct sw_flow *flow;
  953. struct datapath *dp;
  954. struct flow_table *table;
  955. int err;
  956. int key_len;
  957. ovs_lock();
  958. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  959. if (!dp) {
  960. err = -ENODEV;
  961. goto unlock;
  962. }
  963. if (!a[OVS_FLOW_ATTR_KEY]) {
  964. err = flush_flows(dp);
  965. goto unlock;
  966. }
  967. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  968. if (err)
  969. goto unlock;
  970. table = ovsl_dereference(dp->table);
  971. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  972. if (!flow) {
  973. err = -ENOENT;
  974. goto unlock;
  975. }
  976. reply = ovs_flow_cmd_alloc_info(flow);
  977. if (!reply) {
  978. err = -ENOMEM;
  979. goto unlock;
  980. }
  981. ovs_flow_tbl_remove(table, flow);
  982. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  983. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  984. BUG_ON(err < 0);
  985. ovs_flow_deferred_free(flow);
  986. ovs_unlock();
  987. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  988. return 0;
  989. unlock:
  990. ovs_unlock();
  991. return err;
  992. }
  993. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  994. {
  995. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  996. struct datapath *dp;
  997. struct flow_table *table;
  998. ovs_lock();
  999. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1000. if (!dp) {
  1001. ovs_unlock();
  1002. return -ENODEV;
  1003. }
  1004. table = ovsl_dereference(dp->table);
  1005. for (;;) {
  1006. struct sw_flow *flow;
  1007. u32 bucket, obj;
  1008. bucket = cb->args[0];
  1009. obj = cb->args[1];
  1010. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  1011. if (!flow)
  1012. break;
  1013. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  1014. NETLINK_CB(cb->skb).portid,
  1015. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1016. OVS_FLOW_CMD_NEW) < 0)
  1017. break;
  1018. cb->args[0] = bucket;
  1019. cb->args[1] = obj;
  1020. }
  1021. ovs_unlock();
  1022. return skb->len;
  1023. }
  1024. static struct genl_ops dp_flow_genl_ops[] = {
  1025. { .cmd = OVS_FLOW_CMD_NEW,
  1026. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1027. .policy = flow_policy,
  1028. .doit = ovs_flow_cmd_new_or_set
  1029. },
  1030. { .cmd = OVS_FLOW_CMD_DEL,
  1031. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1032. .policy = flow_policy,
  1033. .doit = ovs_flow_cmd_del
  1034. },
  1035. { .cmd = OVS_FLOW_CMD_GET,
  1036. .flags = 0, /* OK for unprivileged users. */
  1037. .policy = flow_policy,
  1038. .doit = ovs_flow_cmd_get,
  1039. .dumpit = ovs_flow_cmd_dump
  1040. },
  1041. { .cmd = OVS_FLOW_CMD_SET,
  1042. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1043. .policy = flow_policy,
  1044. .doit = ovs_flow_cmd_new_or_set,
  1045. },
  1046. };
  1047. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1048. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1049. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1050. };
  1051. static struct genl_family dp_datapath_genl_family = {
  1052. .id = GENL_ID_GENERATE,
  1053. .hdrsize = sizeof(struct ovs_header),
  1054. .name = OVS_DATAPATH_FAMILY,
  1055. .version = OVS_DATAPATH_VERSION,
  1056. .maxattr = OVS_DP_ATTR_MAX,
  1057. .netnsok = true,
  1058. .parallel_ops = true,
  1059. };
  1060. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  1061. .name = OVS_DATAPATH_MCGROUP
  1062. };
  1063. static size_t ovs_dp_cmd_msg_size(void)
  1064. {
  1065. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1066. msgsize += nla_total_size(IFNAMSIZ);
  1067. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1068. return msgsize;
  1069. }
  1070. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1071. u32 portid, u32 seq, u32 flags, u8 cmd)
  1072. {
  1073. struct ovs_header *ovs_header;
  1074. struct ovs_dp_stats dp_stats;
  1075. int err;
  1076. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1077. flags, cmd);
  1078. if (!ovs_header)
  1079. goto error;
  1080. ovs_header->dp_ifindex = get_dpifindex(dp);
  1081. rcu_read_lock();
  1082. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1083. rcu_read_unlock();
  1084. if (err)
  1085. goto nla_put_failure;
  1086. get_dp_stats(dp, &dp_stats);
  1087. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1088. goto nla_put_failure;
  1089. return genlmsg_end(skb, ovs_header);
  1090. nla_put_failure:
  1091. genlmsg_cancel(skb, ovs_header);
  1092. error:
  1093. return -EMSGSIZE;
  1094. }
  1095. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1096. u32 seq, u8 cmd)
  1097. {
  1098. struct sk_buff *skb;
  1099. int retval;
  1100. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  1101. if (!skb)
  1102. return ERR_PTR(-ENOMEM);
  1103. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1104. if (retval < 0) {
  1105. kfree_skb(skb);
  1106. return ERR_PTR(retval);
  1107. }
  1108. return skb;
  1109. }
  1110. /* Called with ovs_mutex. */
  1111. static struct datapath *lookup_datapath(struct net *net,
  1112. struct ovs_header *ovs_header,
  1113. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1114. {
  1115. struct datapath *dp;
  1116. if (!a[OVS_DP_ATTR_NAME])
  1117. dp = get_dp(net, ovs_header->dp_ifindex);
  1118. else {
  1119. struct vport *vport;
  1120. rcu_read_lock();
  1121. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1122. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1123. rcu_read_unlock();
  1124. }
  1125. return dp ? dp : ERR_PTR(-ENODEV);
  1126. }
  1127. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1128. {
  1129. struct nlattr **a = info->attrs;
  1130. struct vport_parms parms;
  1131. struct sk_buff *reply;
  1132. struct datapath *dp;
  1133. struct vport *vport;
  1134. struct ovs_net *ovs_net;
  1135. int err, i;
  1136. err = -EINVAL;
  1137. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1138. goto err;
  1139. ovs_lock();
  1140. err = -ENOMEM;
  1141. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1142. if (dp == NULL)
  1143. goto err_unlock_ovs;
  1144. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1145. /* Allocate table. */
  1146. err = -ENOMEM;
  1147. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1148. if (!dp->table)
  1149. goto err_free_dp;
  1150. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1151. if (!dp->stats_percpu) {
  1152. err = -ENOMEM;
  1153. goto err_destroy_table;
  1154. }
  1155. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1156. GFP_KERNEL);
  1157. if (!dp->ports) {
  1158. err = -ENOMEM;
  1159. goto err_destroy_percpu;
  1160. }
  1161. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1162. INIT_HLIST_HEAD(&dp->ports[i]);
  1163. /* Set up our datapath device. */
  1164. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1165. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1166. parms.options = NULL;
  1167. parms.dp = dp;
  1168. parms.port_no = OVSP_LOCAL;
  1169. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1170. vport = new_vport(&parms);
  1171. if (IS_ERR(vport)) {
  1172. err = PTR_ERR(vport);
  1173. if (err == -EBUSY)
  1174. err = -EEXIST;
  1175. goto err_destroy_ports_array;
  1176. }
  1177. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1178. info->snd_seq, OVS_DP_CMD_NEW);
  1179. err = PTR_ERR(reply);
  1180. if (IS_ERR(reply))
  1181. goto err_destroy_local_port;
  1182. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1183. list_add_tail(&dp->list_node, &ovs_net->dps);
  1184. ovs_unlock();
  1185. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1186. return 0;
  1187. err_destroy_local_port:
  1188. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1189. err_destroy_ports_array:
  1190. kfree(dp->ports);
  1191. err_destroy_percpu:
  1192. free_percpu(dp->stats_percpu);
  1193. err_destroy_table:
  1194. ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
  1195. err_free_dp:
  1196. release_net(ovs_dp_get_net(dp));
  1197. kfree(dp);
  1198. err_unlock_ovs:
  1199. ovs_unlock();
  1200. err:
  1201. return err;
  1202. }
  1203. /* Called with ovs_mutex. */
  1204. static void __dp_destroy(struct datapath *dp)
  1205. {
  1206. int i;
  1207. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1208. struct vport *vport;
  1209. struct hlist_node *n;
  1210. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1211. if (vport->port_no != OVSP_LOCAL)
  1212. ovs_dp_detach_port(vport);
  1213. }
  1214. list_del(&dp->list_node);
  1215. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1216. * all port in datapath are destroyed first before freeing datapath.
  1217. */
  1218. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1219. call_rcu(&dp->rcu, destroy_dp_rcu);
  1220. }
  1221. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1222. {
  1223. struct sk_buff *reply;
  1224. struct datapath *dp;
  1225. int err;
  1226. ovs_lock();
  1227. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1228. err = PTR_ERR(dp);
  1229. if (IS_ERR(dp))
  1230. goto unlock;
  1231. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1232. info->snd_seq, OVS_DP_CMD_DEL);
  1233. err = PTR_ERR(reply);
  1234. if (IS_ERR(reply))
  1235. goto unlock;
  1236. __dp_destroy(dp);
  1237. ovs_unlock();
  1238. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1239. return 0;
  1240. unlock:
  1241. ovs_unlock();
  1242. return err;
  1243. }
  1244. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1245. {
  1246. struct sk_buff *reply;
  1247. struct datapath *dp;
  1248. int err;
  1249. ovs_lock();
  1250. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1251. err = PTR_ERR(dp);
  1252. if (IS_ERR(dp))
  1253. goto unlock;
  1254. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1255. info->snd_seq, OVS_DP_CMD_NEW);
  1256. if (IS_ERR(reply)) {
  1257. err = PTR_ERR(reply);
  1258. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1259. ovs_dp_datapath_multicast_group.id, err);
  1260. err = 0;
  1261. goto unlock;
  1262. }
  1263. ovs_unlock();
  1264. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1265. return 0;
  1266. unlock:
  1267. ovs_unlock();
  1268. return err;
  1269. }
  1270. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1271. {
  1272. struct sk_buff *reply;
  1273. struct datapath *dp;
  1274. int err;
  1275. ovs_lock();
  1276. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1277. if (IS_ERR(dp)) {
  1278. err = PTR_ERR(dp);
  1279. goto unlock;
  1280. }
  1281. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1282. info->snd_seq, OVS_DP_CMD_NEW);
  1283. if (IS_ERR(reply)) {
  1284. err = PTR_ERR(reply);
  1285. goto unlock;
  1286. }
  1287. ovs_unlock();
  1288. return genlmsg_reply(reply, info);
  1289. unlock:
  1290. ovs_unlock();
  1291. return err;
  1292. }
  1293. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1294. {
  1295. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1296. struct datapath *dp;
  1297. int skip = cb->args[0];
  1298. int i = 0;
  1299. ovs_lock();
  1300. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1301. if (i >= skip &&
  1302. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1303. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1304. OVS_DP_CMD_NEW) < 0)
  1305. break;
  1306. i++;
  1307. }
  1308. ovs_unlock();
  1309. cb->args[0] = i;
  1310. return skb->len;
  1311. }
  1312. static struct genl_ops dp_datapath_genl_ops[] = {
  1313. { .cmd = OVS_DP_CMD_NEW,
  1314. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1315. .policy = datapath_policy,
  1316. .doit = ovs_dp_cmd_new
  1317. },
  1318. { .cmd = OVS_DP_CMD_DEL,
  1319. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1320. .policy = datapath_policy,
  1321. .doit = ovs_dp_cmd_del
  1322. },
  1323. { .cmd = OVS_DP_CMD_GET,
  1324. .flags = 0, /* OK for unprivileged users. */
  1325. .policy = datapath_policy,
  1326. .doit = ovs_dp_cmd_get,
  1327. .dumpit = ovs_dp_cmd_dump
  1328. },
  1329. { .cmd = OVS_DP_CMD_SET,
  1330. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1331. .policy = datapath_policy,
  1332. .doit = ovs_dp_cmd_set,
  1333. },
  1334. };
  1335. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1336. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1337. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1338. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1339. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1340. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1341. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1342. };
  1343. static struct genl_family dp_vport_genl_family = {
  1344. .id = GENL_ID_GENERATE,
  1345. .hdrsize = sizeof(struct ovs_header),
  1346. .name = OVS_VPORT_FAMILY,
  1347. .version = OVS_VPORT_VERSION,
  1348. .maxattr = OVS_VPORT_ATTR_MAX,
  1349. .netnsok = true,
  1350. .parallel_ops = true,
  1351. };
  1352. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1353. .name = OVS_VPORT_MCGROUP
  1354. };
  1355. /* Called with ovs_mutex or RCU read lock. */
  1356. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1357. u32 portid, u32 seq, u32 flags, u8 cmd)
  1358. {
  1359. struct ovs_header *ovs_header;
  1360. struct ovs_vport_stats vport_stats;
  1361. int err;
  1362. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1363. flags, cmd);
  1364. if (!ovs_header)
  1365. return -EMSGSIZE;
  1366. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1367. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1368. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1369. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1370. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1371. goto nla_put_failure;
  1372. ovs_vport_get_stats(vport, &vport_stats);
  1373. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1374. &vport_stats))
  1375. goto nla_put_failure;
  1376. err = ovs_vport_get_options(vport, skb);
  1377. if (err == -EMSGSIZE)
  1378. goto error;
  1379. return genlmsg_end(skb, ovs_header);
  1380. nla_put_failure:
  1381. err = -EMSGSIZE;
  1382. error:
  1383. genlmsg_cancel(skb, ovs_header);
  1384. return err;
  1385. }
  1386. /* Called with ovs_mutex or RCU read lock. */
  1387. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1388. u32 seq, u8 cmd)
  1389. {
  1390. struct sk_buff *skb;
  1391. int retval;
  1392. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1393. if (!skb)
  1394. return ERR_PTR(-ENOMEM);
  1395. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1396. BUG_ON(retval < 0);
  1397. return skb;
  1398. }
  1399. /* Called with ovs_mutex or RCU read lock. */
  1400. static struct vport *lookup_vport(struct net *net,
  1401. struct ovs_header *ovs_header,
  1402. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1403. {
  1404. struct datapath *dp;
  1405. struct vport *vport;
  1406. if (a[OVS_VPORT_ATTR_NAME]) {
  1407. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1408. if (!vport)
  1409. return ERR_PTR(-ENODEV);
  1410. if (ovs_header->dp_ifindex &&
  1411. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1412. return ERR_PTR(-ENODEV);
  1413. return vport;
  1414. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1415. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1416. if (port_no >= DP_MAX_PORTS)
  1417. return ERR_PTR(-EFBIG);
  1418. dp = get_dp(net, ovs_header->dp_ifindex);
  1419. if (!dp)
  1420. return ERR_PTR(-ENODEV);
  1421. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1422. if (!vport)
  1423. return ERR_PTR(-ENODEV);
  1424. return vport;
  1425. } else
  1426. return ERR_PTR(-EINVAL);
  1427. }
  1428. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1429. {
  1430. struct nlattr **a = info->attrs;
  1431. struct ovs_header *ovs_header = info->userhdr;
  1432. struct vport_parms parms;
  1433. struct sk_buff *reply;
  1434. struct vport *vport;
  1435. struct datapath *dp;
  1436. u32 port_no;
  1437. int err;
  1438. err = -EINVAL;
  1439. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1440. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1441. goto exit;
  1442. ovs_lock();
  1443. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1444. err = -ENODEV;
  1445. if (!dp)
  1446. goto exit_unlock;
  1447. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1448. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1449. err = -EFBIG;
  1450. if (port_no >= DP_MAX_PORTS)
  1451. goto exit_unlock;
  1452. vport = ovs_vport_ovsl(dp, port_no);
  1453. err = -EBUSY;
  1454. if (vport)
  1455. goto exit_unlock;
  1456. } else {
  1457. for (port_no = 1; ; port_no++) {
  1458. if (port_no >= DP_MAX_PORTS) {
  1459. err = -EFBIG;
  1460. goto exit_unlock;
  1461. }
  1462. vport = ovs_vport_ovsl(dp, port_no);
  1463. if (!vport)
  1464. break;
  1465. }
  1466. }
  1467. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1468. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1469. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1470. parms.dp = dp;
  1471. parms.port_no = port_no;
  1472. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1473. vport = new_vport(&parms);
  1474. err = PTR_ERR(vport);
  1475. if (IS_ERR(vport))
  1476. goto exit_unlock;
  1477. err = 0;
  1478. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1479. OVS_VPORT_CMD_NEW);
  1480. if (IS_ERR(reply)) {
  1481. err = PTR_ERR(reply);
  1482. ovs_dp_detach_port(vport);
  1483. goto exit_unlock;
  1484. }
  1485. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1486. exit_unlock:
  1487. ovs_unlock();
  1488. exit:
  1489. return err;
  1490. }
  1491. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1492. {
  1493. struct nlattr **a = info->attrs;
  1494. struct sk_buff *reply;
  1495. struct vport *vport;
  1496. int err;
  1497. ovs_lock();
  1498. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1499. err = PTR_ERR(vport);
  1500. if (IS_ERR(vport))
  1501. goto exit_unlock;
  1502. if (a[OVS_VPORT_ATTR_TYPE] &&
  1503. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1504. err = -EINVAL;
  1505. goto exit_unlock;
  1506. }
  1507. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1508. if (!reply) {
  1509. err = -ENOMEM;
  1510. goto exit_unlock;
  1511. }
  1512. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1513. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1514. if (err)
  1515. goto exit_free;
  1516. }
  1517. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1518. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1519. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1520. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1521. BUG_ON(err < 0);
  1522. ovs_unlock();
  1523. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1524. return 0;
  1525. rtnl_unlock();
  1526. return 0;
  1527. exit_free:
  1528. kfree_skb(reply);
  1529. exit_unlock:
  1530. ovs_unlock();
  1531. return err;
  1532. }
  1533. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1534. {
  1535. struct nlattr **a = info->attrs;
  1536. struct sk_buff *reply;
  1537. struct vport *vport;
  1538. int err;
  1539. ovs_lock();
  1540. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1541. err = PTR_ERR(vport);
  1542. if (IS_ERR(vport))
  1543. goto exit_unlock;
  1544. if (vport->port_no == OVSP_LOCAL) {
  1545. err = -EINVAL;
  1546. goto exit_unlock;
  1547. }
  1548. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1549. OVS_VPORT_CMD_DEL);
  1550. err = PTR_ERR(reply);
  1551. if (IS_ERR(reply))
  1552. goto exit_unlock;
  1553. err = 0;
  1554. ovs_dp_detach_port(vport);
  1555. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1556. exit_unlock:
  1557. ovs_unlock();
  1558. return err;
  1559. }
  1560. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1561. {
  1562. struct nlattr **a = info->attrs;
  1563. struct ovs_header *ovs_header = info->userhdr;
  1564. struct sk_buff *reply;
  1565. struct vport *vport;
  1566. int err;
  1567. rcu_read_lock();
  1568. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1569. err = PTR_ERR(vport);
  1570. if (IS_ERR(vport))
  1571. goto exit_unlock;
  1572. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1573. OVS_VPORT_CMD_NEW);
  1574. err = PTR_ERR(reply);
  1575. if (IS_ERR(reply))
  1576. goto exit_unlock;
  1577. rcu_read_unlock();
  1578. return genlmsg_reply(reply, info);
  1579. exit_unlock:
  1580. rcu_read_unlock();
  1581. return err;
  1582. }
  1583. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1584. {
  1585. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1586. struct datapath *dp;
  1587. int bucket = cb->args[0], skip = cb->args[1];
  1588. int i, j = 0;
  1589. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1590. if (!dp)
  1591. return -ENODEV;
  1592. rcu_read_lock();
  1593. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1594. struct vport *vport;
  1595. j = 0;
  1596. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1597. if (j >= skip &&
  1598. ovs_vport_cmd_fill_info(vport, skb,
  1599. NETLINK_CB(cb->skb).portid,
  1600. cb->nlh->nlmsg_seq,
  1601. NLM_F_MULTI,
  1602. OVS_VPORT_CMD_NEW) < 0)
  1603. goto out;
  1604. j++;
  1605. }
  1606. skip = 0;
  1607. }
  1608. out:
  1609. rcu_read_unlock();
  1610. cb->args[0] = i;
  1611. cb->args[1] = j;
  1612. return skb->len;
  1613. }
  1614. static struct genl_ops dp_vport_genl_ops[] = {
  1615. { .cmd = OVS_VPORT_CMD_NEW,
  1616. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1617. .policy = vport_policy,
  1618. .doit = ovs_vport_cmd_new
  1619. },
  1620. { .cmd = OVS_VPORT_CMD_DEL,
  1621. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1622. .policy = vport_policy,
  1623. .doit = ovs_vport_cmd_del
  1624. },
  1625. { .cmd = OVS_VPORT_CMD_GET,
  1626. .flags = 0, /* OK for unprivileged users. */
  1627. .policy = vport_policy,
  1628. .doit = ovs_vport_cmd_get,
  1629. .dumpit = ovs_vport_cmd_dump
  1630. },
  1631. { .cmd = OVS_VPORT_CMD_SET,
  1632. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1633. .policy = vport_policy,
  1634. .doit = ovs_vport_cmd_set,
  1635. },
  1636. };
  1637. struct genl_family_and_ops {
  1638. struct genl_family *family;
  1639. struct genl_ops *ops;
  1640. int n_ops;
  1641. struct genl_multicast_group *group;
  1642. };
  1643. static const struct genl_family_and_ops dp_genl_families[] = {
  1644. { &dp_datapath_genl_family,
  1645. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1646. &ovs_dp_datapath_multicast_group },
  1647. { &dp_vport_genl_family,
  1648. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1649. &ovs_dp_vport_multicast_group },
  1650. { &dp_flow_genl_family,
  1651. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1652. &ovs_dp_flow_multicast_group },
  1653. { &dp_packet_genl_family,
  1654. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1655. NULL },
  1656. };
  1657. static void dp_unregister_genl(int n_families)
  1658. {
  1659. int i;
  1660. for (i = 0; i < n_families; i++)
  1661. genl_unregister_family(dp_genl_families[i].family);
  1662. }
  1663. static int dp_register_genl(void)
  1664. {
  1665. int n_registered;
  1666. int err;
  1667. int i;
  1668. n_registered = 0;
  1669. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1670. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1671. err = genl_register_family_with_ops(f->family, f->ops,
  1672. f->n_ops);
  1673. if (err)
  1674. goto error;
  1675. n_registered++;
  1676. if (f->group) {
  1677. err = genl_register_mc_group(f->family, f->group);
  1678. if (err)
  1679. goto error;
  1680. }
  1681. }
  1682. return 0;
  1683. error:
  1684. dp_unregister_genl(n_registered);
  1685. return err;
  1686. }
  1687. static void rehash_flow_table(struct work_struct *work)
  1688. {
  1689. struct datapath *dp;
  1690. struct net *net;
  1691. ovs_lock();
  1692. rtnl_lock();
  1693. for_each_net(net) {
  1694. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1695. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1696. struct flow_table *old_table = ovsl_dereference(dp->table);
  1697. struct flow_table *new_table;
  1698. new_table = ovs_flow_tbl_rehash(old_table);
  1699. if (!IS_ERR(new_table)) {
  1700. rcu_assign_pointer(dp->table, new_table);
  1701. ovs_flow_tbl_deferred_destroy(old_table);
  1702. }
  1703. }
  1704. }
  1705. rtnl_unlock();
  1706. ovs_unlock();
  1707. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1708. }
  1709. static int __net_init ovs_init_net(struct net *net)
  1710. {
  1711. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1712. INIT_LIST_HEAD(&ovs_net->dps);
  1713. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1714. return 0;
  1715. }
  1716. static void __net_exit ovs_exit_net(struct net *net)
  1717. {
  1718. struct datapath *dp, *dp_next;
  1719. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1720. ovs_lock();
  1721. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1722. __dp_destroy(dp);
  1723. ovs_unlock();
  1724. cancel_work_sync(&ovs_net->dp_notify_work);
  1725. }
  1726. static struct pernet_operations ovs_net_ops = {
  1727. .init = ovs_init_net,
  1728. .exit = ovs_exit_net,
  1729. .id = &ovs_net_id,
  1730. .size = sizeof(struct ovs_net),
  1731. };
  1732. static int __init dp_init(void)
  1733. {
  1734. int err;
  1735. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1736. pr_info("Open vSwitch switching datapath\n");
  1737. err = ovs_flow_init();
  1738. if (err)
  1739. goto error;
  1740. err = ovs_vport_init();
  1741. if (err)
  1742. goto error_flow_exit;
  1743. err = register_pernet_device(&ovs_net_ops);
  1744. if (err)
  1745. goto error_vport_exit;
  1746. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1747. if (err)
  1748. goto error_netns_exit;
  1749. err = dp_register_genl();
  1750. if (err < 0)
  1751. goto error_unreg_notifier;
  1752. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1753. return 0;
  1754. error_unreg_notifier:
  1755. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1756. error_netns_exit:
  1757. unregister_pernet_device(&ovs_net_ops);
  1758. error_vport_exit:
  1759. ovs_vport_exit();
  1760. error_flow_exit:
  1761. ovs_flow_exit();
  1762. error:
  1763. return err;
  1764. }
  1765. static void dp_cleanup(void)
  1766. {
  1767. cancel_delayed_work_sync(&rehash_flow_wq);
  1768. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1769. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1770. unregister_pernet_device(&ovs_net_ops);
  1771. rcu_barrier();
  1772. ovs_vport_exit();
  1773. ovs_flow_exit();
  1774. }
  1775. module_init(dp_init);
  1776. module_exit(dp_cleanup);
  1777. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1778. MODULE_LICENSE("GPL");